Remove character from string

This is a discussion on Remove character from string within the C Programming forums, part of the General Programming Boards category; Hi all, the object is to write a function called say remchar that recieves a string say 'Hello' and a ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    7

    Remove character from string

    Hi all,
    the object is to write a function called say remchar that recieves a string say 'Hello' and a character 'o'.
    The function should loop through the string look for the character passed 'o' if it exists which it does, rmove the character from the string and pass it pack to main to print out the modified string.

    here is what I have so far and won't compile.


    Code:
    #include <stdio.h>
    
    int main()
    {
    	char strin[10]={'H','e','l','l','o'};  	//init a char string called strin
    	char chr = 'o';				//character var to pass to function to look for
    						
    	printf("\nstring before function call\t:>%s", strin);		//print the string pre call
    
    	remchar(strin, chr);						//call remchar func 
    
    	printf("\nstring after function call\t:>%s\n", strin);		//string post call
    
    	return 0;
    }
    
    remchar(strin, chr)
    {
    int i;
    	for(i = 0; strin[i] != '\0'; i++)	//start for loop
    	{
    		
    	if(strin[i] == chr)		//test for character
    			{
    			strin[i] = strin[i] + 1 ; // write next character back to current position
    			}
    		}
    }
    The compile error is
    subscripted value is niether array nor pointer
    .
    I haven't seen this one before!

    Also can any one give me a bit of clarity on how to handle modifying arrays etc? I am not looking at pointers yet!

    Any help is much appreciated

    Thanks

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You need to fix your function. You also need to declare it, or prototype it, before you use it. Look at your function. What type is strin? What about chr? Your function doesn't know, because you don't tell it.


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

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    6
    Looks to me like he came from another programming language, probably a web one.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    7

    Remove character from string

    Hi quzah
    ok thankyou understand that now I have made change to the function and declared so that it compiles. When run it prints Hellp because when 'o' is encountered I have added 1 to the value which makes 'o' become 'p' 'Hellp' how ironic!

    This is where I probably require some clarity on updating the string before returning, because my test works by finding the character I am looking for its the how to manipulate this string is my issue given the objective stated earlier!

    Here is my updated code to address the function issues!
    Code:
    #include <stdio.h>
    void remchar(char *strin, char chr);
    int main()
    {
    	char s[10]={'H','e','l','l','o'};  					//init a char string
    	char c = 'o';								//character var to pass to function to look for
    						
    	printf("\nstring before function call\t:>%s", s);		//print the string pre call
    
    	remchar(s, c);								//call remchar func 
    
    	printf("\nstring after function call\t:>%s\n", s);		//string post call
    
    	return 0;
    }
    
    void remchar(char *strin, char chr)
    {
    int i;
    	for(i = 0; strin[i] != '\0'; i++)	//start for loop	
    	{
    		
    	if(strin[i] == chr)			//test for character
    			{
    			strin[i] = strin[i] + 1;//this is wrong!!!!
    			}
    		}
    }
    again thanx for input and guidance

  5. #5
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    The variable strin is not a c-string just an array of char's. So testing for the '\0' terminator in remchar() will not work.
    Kurt

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by ZuK
    The variable strin is not a c-string just an array of char's. So testing for the '\0' terminator in remchar() will not work.
    Although it might look that way at first glance, such is not the case.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by Dave_Sinkula
    Although it might look that way at first glance, such is not the case.
    Yeah got it. It's declared to hold 10 char's so the remaining characters are initialized to '\0'.
    Still a funny way to declare a c-string
    Kurt

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    7

    Remove character from string

    ok I have now made some changes to the function to process the incoming string write each element to an output string and copy the updated string back to original.

    However when it runs I get out as follows

    Hell?

    where does the question mark come from?

    can any one offer me some concise accurate answer?
    can anyone comment constructively on whether this is a good way of handling this scenario and offer advise?

    many thanks and here is the code
    Code:
    #include <stdio.h>
    #include <strings.h>
    void remchar(char *strin, char chr);
    int main()
    {
    	char s[10]={'H','e','l','l','o'};  	//init a char string
    	char c = 'o';				//character var to pass to function to look for
    						
    	printf("\nstring before function call\t:>%s", s);		//print the string pre call
    
    	remchar(s, c);							//call remchar func 
    
    	printf("\nstring after function call\t:>%s\n", s);		//string post call
    
    	return 0;
    }
    
    void remchar(char *strin, char chr)
    {
    int i, j=0;
    char cupdate[10];				//temp storage for updated string
    
    	for(i = 0; strin[i] != '\0'; i++)	//start for loop
    	{
    		
    	if(strin[i] != chr)			//check its not the char i want
    			{
    			cupdate[j] = strin[i];	//copy element to temp string 
    			j++;			//move along the index one
    			}
    	else
    			{
    			;			//do nothing
    			}
    	}
    j++;						//move along the index one
    cupdate[j] = '\0';				//terminate the string
    printf("strin = %s\tcupdate = %s", strin, cupdate);	//show what its doing
    strcpy(strin, cupdate);					//copy updated string back to original
    }

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Don't increment j after the loop. It's already where you want it to be to null terminate the string.

    [edit]And it's <string.h>, not <strings.h>.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  10. #10
    Registered User
    Join Date
    May 2006
    Posts
    7
    Thankyou Dave_Sinkula
    for your advice and guidance.

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Consider also that you can modify the original string in place, thus getting away from using a temporary.
    Code:
    #include <stdio.h>
    
    void remchar(char *s, char chr)
    {
       int i, j = 0;
       for ( i = 0; s[i] != '\0'; i++ ) /* 'i' moves through all of original 's' */
       {
          if ( s[i] != chr )
          {
             s[j++] = s[i]; /* 'j' only moves after we write a non-'chr' */
          }
       }
       s[j] = '\0'; /* re-null-terminate */
    }
    
    int main()
    {
       char text[] = "The quick brown fox jumps over the lazy dog.";
       printf("before : %s\n", text);
       remchar(text, 'o');
       printf("after  : %s\n", text);
       return 0;
    }
    
    /* my output 
    before : The quick brown fox jumps over the lazy dog.
    after  : The quick brwn fx jumps ver the lazy dg.
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  12. #12
    Registered User
    Join Date
    May 2006
    Posts
    7
    Ahh That is the kind of guidance I was looking for Dave_Sinkula, thankyou, the copy inplace looks a more economic way of coding it, that is what I wanted to do but new not how to express it or how to code it, I will use that example now!

    Cheers

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. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21