i dunno im sure this output the good result i dont understand why it got not the output that i want

#include <stdio.h>
void Squeeze2(char *str,char *save)
{
     int i,x;
     int buffer=0;//used to save stuff
     for( i=0 , x=0 ; str[i]!=0 && save[x]!=0 ; i++, x++ ) {
          if( str[i]!=save[x] ) {
              str[buffer]=str[i];
              buffer++;
              }
          }
}
int main(void)
{
    char name[]="NAME";
    Squeeze2(name,"NAM");
    puts(name);
    return getchar();
}

Recommended Answers

All 9 Replies

Why don't you start by giving us a less vague question. What is the program supposed to do? What output are you expecting? Without these details it's hard for us to help.

This code is weird. You pass two strings, and check if they are equal, if not storing it in the "buffer position", suppose position 0 is already filled, you are overwriting it. Why do you use two counter variables when it is not necessary??? A lot of changes is required.

The code is not only weird its senseless. Firstly two pointers buffer and i manipulating the same char array so what it writes in the array in which condition is completely unpredictable it just depends on both the inputs.
And even by going by the function name "squeeze2" if the OP wants to squeeze the first or the second array then why so much redundant work it can even be achieved by inbuilt functions.

>i dunno im sure this output the good result
Did you actually write this?

>i dont understand why it got not the output that i want
Explain us what output you are expecting, then it's easier for us to help you fixing the problem :) [B]return getchar();[/B] , it's not good to write it like that, it's better to split it into two separate instructions like:

getchar();
return 0;
commented: "Did you write it?" Same question came to my mind too !!! ;) +2

well i m trying to delete every character from String2 and delete that in string1

and yes i wrote it
well first i read both string then after that make a test saying every characters thats not in search and i do new string in str[buffer].

Well in that case say you have 6 characters in str array,then assuming that you overwrite first three characters in the exact way in which you want even then you are doing nothing with the left over's.
Well change your code as this :

#include <stdio.h>
void Squeeze2(char *str,char *save,char *required)
{
     int i,x;
     int buffer=0;//used to save stuff
// And ya make an habit of using null character rather than simple 0
     for( i=0 , x=0 ; str[i]!='\0' && save[x]!='\0' ; i++, x++ ) {
          if( str[i]!=save[x] ) {
              required[buffer]=str[i];
              buffer++;
              }
          }
}
int main(void)
{
    char name[]="NAME";
    char req[10];
    Squeeze2(name,"NAM",req);
    puts(req);
    getchar();
    return 0;
}

Well in that case say you have 6 characters in str array,then assuming that you overwrite first three characters in the exact way in which you want even then you are doing nothing with the left over's.
Well change your code as this :

#include <stdio.h>
void Squeeze2(char *str,char *save,char *required)
{
     int i,x;
     int buffer=0;//used to save stuff
// And ya make an habit of using null character rather than simple 0
     for( i=0 , x=0 ; str[i]!='\0' && save[x]!='\0' ; i++, x++ ) {
          if( str[i]!=save[x] ) {
              required[buffer]=str[i];
              buffer++;
              }
          }
}
int main(void)
{
    char name[]="NAME";
    char req[10];
    Squeeze2(name,"NAM",req);
    puts(req);
    getchar();
    return 0;
}

oh thanks man but coudlnt i change direct to buffer?

> oh thanks man but coudlnt i change direct to buffer?
What directly to buffer ? You mean to say directly to str array right ? I wont say you can't . First you need to write it as you were writing earlier and then after it you need to clear all the memory blocks ahead of it.
That is if your squeezing leaves your str array at index 4 then you need to find the total memory blocks allocated for this array in main function(because thats where you are allocating it memory)and clear all the memory blocks allocated for this block ahead. (Which is the right "FORMAL" procedure...).

Shortcut :
After your str array is over written just move the index 1 block ahead and place '\0' character in it. Placing the '\0' character symbolises the end of string and compiler won't print ahead.

But be warned this method is sure to bring one or the other problem because you won't know where exactly to place the '\0' character.

And ya your program has whole lot of limitations :
1> Your program doesn't take every letter in string2 and delete it in string1.Because its not looping its just matching the indexes. If instead of NAM as second string i give MAN then even with these letters in string1 your code wont delete it because its just matching indexes.

In total :
Try to write a better code for your requisite because at this moment your code is not serving your purpose even an inch.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.