Thread: Function problem

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    187

    Function problem

    hello guys today i had problem in my squeeze function i dunno why it does not change anything i even tested with debug code it worked well i m trying to delete every characters from string that match the other string
    Code:
    void Squeeze(char *in,char *save)
    {
    	int i,x,j;
    	i=x=j=0;
    	while(in[x]!=save[i]){
    		 puts("While loop is ran!");
    		 in[j]=in[x];
    		 i++;x++;j++;
    	}
    }
    int main(void)
    {
        char name[]="HELLO WORLD";
    
        Squeeze(name,"WORLD");
        puts(name);
        return getchar();
    }

  2. #2
    Registered User
    Join Date
    Nov 2004
    Location
    USA
    Posts
    516
    Have you tried a dry run of your code on a piece of paper? If you do that, you will instantly realize that your algorithm is wrong.
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your while loop needs to run once for every letter in the shorter of the two strings. Including string.h and using strlen() will help you a lot.

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    but when i tried to add 3rd variable to put the stuff at it worked
    Code:
    void Squeeze(char *in,char *save,char *buffer)
    {
    	int i,x,j,z;
    	i=x=j=0,z=0;
    	while(in[x]!=save[i]){
    		 printf("While loop is ran! %d\n",z);
    		 buffer[j++]=in[x];
    		 buffer[j]=0;
    		 x++;i++;
    	}
    }

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If by "delete every characters from string that match the other string" you mean to copy over to the output buffer, in order of appearance in the source string, only those characters that do not match any of those in the other string, then your algorithm is incorrect.

    Besides the algorithm, both in and save should be declared as pointers to const char, and as Adak noted you should take into account the length of the strings (either directly after using strlen(), or by checking for the respective null terminators).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    yah i solved the problem srry was away

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 10-29-2008, 06:33 AM
  2. wxWidgets link problem
    By cboard_member in forum C++ Programming
    Replies: 2
    Last Post: 02-11-2006, 02:36 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Problem with function pointers
    By vNvNation in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2004, 06:49 AM