Thread: One more string question

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    19

    One more string question

    Say I had a string like this: "blah x blah". How could I remove the x, so it was just "blah blah"? Thanks.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That depends on what "blah" "x" and "blah" are supposed to be. You could always use sscanf and printf.

    sscanf( buffer "%s %*c %s", string1, string2 );
    printf("%s %s", string1, string2 );

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User MisterBadger's Avatar
    Join Date
    Mar 2002
    Posts
    16
    This code seems to work for any number of spaces either side of the 'x'... as long as the "change" array is long enough, of course...

    Code:
    #include<stdio.h>
    #include<string.h>
    
    int main()
    {
     char test[]={"blah x blah"},
             change[20]={"\0"};
    
     int j, i, len=0, count=0;
    
     printf("Test string was  : %s\n\n",test);
    
     len=strlen(test);
    
     for(i=0;i<len;i++)
      {
    	if(test[i]=='x') /* change the 'x' to a space */
    	 test[i]=' ';
    	}
    
     for(j=0,i=0;i<len;j++,i++)
       {
           if(test[i]!=' ')
               change[j]=test[i];
    
           else
             {
              count++;
           if(count==1) /* copy first space only */
             change[j]=test[i];
           else
              j--;
             }
    
         } /* end for */
    
     printf("Changed string is: %s\n\nEnd of program!",change);
    
     return 0;
    }
    Good night all!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Reusing a string pointer question
    By chiefmonkey in forum C++ Programming
    Replies: 3
    Last Post: 05-06-2009, 04:53 PM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. String array question
    By gogo in forum C++ Programming
    Replies: 6
    Last Post: 12-08-2001, 06:44 PM