Simple C code problem

This is a discussion on Simple C code problem within the C Programming forums, part of the General Programming Boards category; Hi guys, wondering If I could have some help, I am trying to design a simple program that will allow ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    6

    Simple C code problem

    Hi guys, wondering If I could have some help, I am trying to design a simple program that will allow a user to enter some text, then the program will read the text and change the letter m to the letter S. at the moment I can get it to display a text string that has a letter m in it, yet dont know how to change it to a s, Im sure its a simple line of code I need, Just starting off in C so any help would be greatly appriciated

    Code:
    #include <string.h>
    
    int main( void )
    {
       char text[ 256 ] = { '\0' };
       int found = 0;
       int counter;
    
       printf( "Please enter a string: " );
       scanf( "%s", text );
    
       for ( counter = 0; counter < strlen( text ); counter++ )
       {
          if ( text[ counter ] == 'm' )
          {
             found = 1;
             break;
          }
       }
       
       if ( found )
          printf( "\n%s", text );
       else
          printf( "\nNot found" );
    }
    Craig

  2. #2
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    here's a function you can call as str_repall(text,'m','S'):

    Code:
    void str_repall(char *text,char old,char new)
    {
      char *c;
      c = text;
      for (c=text;c;c++) {  //test that current!='\0'
        if (*c == old)
          *c = new;
      }
    }

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,632
    You seem close. Instead of setting a flag upon finding the first 'm', assign 'S' when you have found an 'm', and just continue instead of using a break.

    By the way, instead of this:
    Code:
    scanf( "%s", text );
    you should write:
    Code:
    scanf( "%255s", text );
    since you only have enough space for 255 characters, excluding the null character.

    Also, instead of calling strlen() on each iteration of the loop, just call it once. Note that the return type of strlen() is size_t, not int, though you can get away with int.

    You should #include <stdio.h> for printf and scanf.

    EDIT:
    If you do not understand bernt's code, just ignore it. It has a critical bug, anyway. If you do understand it, then note that the important idea is to test for the null character directly instead of using strlen(). (Okay, there is another important idea: to make the code reusable by putting it into a function.)
    Last edited by laserlight; 05-15-2009 at 07:27 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    6

    Smile

    Here is what I have done so far, Not sure on how to use the bit of the code given , I think im nearly there but not sure, here is what I have done now with that other bit of code, yet it fails with following problems

    Code:
    #include <string.h>
    #include <stdio.h>
    
    int main( void )
    {
       char text[ 256 ] = { '\0' };
       int found = 0;
       int counter;
    
       printf( "Please enter a string: " );
       scanf( "%255s", text );
       for ( counter = 0; counter < strlen( text ); counter++ )
       {
          if ( text[ counter ] == 'm' )
          {
             found = 1;
             break;
          }
       }
       
     void str_repall(char *text,char old,char new)
    {
      char *c;
      c = text;
      for (c=text;c;c++) {  //test that current!='\0'
        if (*c == old)
          *c = new;
      }
    }

    error: invalid operands for equal operator ==
    error: syntax error before or at line 26
    : if (*c == old)
    : if (*c == old)<== ???
    error: missing '}'
    error: in program ending, may be missing }
    ch in free(): warning: chunk is already free
    error: cannot recover from previous errors
    at line 30


    Cheers
    Last edited by neo28; 05-15-2009 at 07:34 AM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,632
    Quote Originally Posted by neo28
    Here is what I have done so far, Not sure on how to use the bit of the code given
    Have you learnt how to define a function?

    Do you understand how bernt's function is supposed to work? Do you understand pointer notation? If not, you will never be able to fix bernt's bug, and more importantly, you would have learnt nothing from bernt's suggestion.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    6
    Quote Originally Posted by laserlight View Post
    Have you learnt how to define a function?

    Do you understand how bernt's function is supposed to work? Do you understand pointer notation? If not, you will never be able to fix bernt's bug, and more importantly, you would have learnt nothing from bernt's suggestion.
    I'm only starting off so I know some of the basic functions yet not everything, not sure on pointers etc, is there a simple line of code I can incorporate to do the replace ?

    thanks for all the help

    Craig

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,632
    Quote Originally Posted by neo28
    is there a simple line of code I can incorporate to do the replace ?
    strchr() can help you a little, but take a look at what you already have now:
    Code:
    for ( counter = 0; counter < strlen( text ); counter++ )
    {
        if ( text[ counter ] == 'm' )
        {
            found = 1;
            break;
        }
    }
    Suppose that instead of setting found to 1 and then breaking from the loop, you assigned 'S' to text[counter]. Would that not accomplish what you want to do?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Apr 2009
    Posts
    139
    Following function needs to be called from.

    Replace your for loop with this line below.
    str_repall(text,m,s);
    Code:
    void str_repall(char *text,char old,char new)
    
    int main( void )
    {
       char text[ 256 ] = { '\0' };
       int found = 0;
       int counter;
    
       printf( "Please enter a string: " );
       scanf( "%255s", text );
       str_repall(text,m,s);       //Call  fuction replace
       }

  9. #9
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by neo28 View Post
    Here is what I have done so far, Not sure on how to use the bit of the code given , I think im nearly there but not sure, here is what I have done now with that other bit of code, yet it fails with following problems

    Code:
    #include <string.h>
    #include <stdio.h>
    
    int main( void )
    {
       char text[ 256 ] = { '\0' };
       int found = 0;
       int counter;
    
       printf( "Please enter a string: " );
       scanf( "%255s", text );
       for ( counter = 0; counter < strlen( text ); counter++ )
       {
          if ( text[ counter ] == 'm' )
          {
             found = 1;
             break;
          }
       }
       
     void str_repall(char *text,char old,char new)
    {
      char *c;
      c = text;
      for (c=text;c;c++) {  //test that current!='\0'
        if (*c == old)
          *c = new;
      }
    }

    error: invalid operands for equal operator ==
    error: syntax error before or at line 26
    : if (*c == old)
    : if (*c == old)<== ???
    error: missing '}'
    error: in program ending, may be missing }
    ch in free(): warning: chunk is already free
    error: cannot recover from previous errors
    at line 30


    Cheers
    There is no need to use such a function if you simply want to replace each occurence of 'm' with 's'. Just in your 'if' body put
    Code:
    text[ counter ] = 's'
    .
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    60
    this is better version:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define MAX_BUFFER		256
    
    int main( int argc, char **argv )
    {
    	char *input;
    	char *p;
    	char *o;
    	
    	input = (char *) calloc( MAX_BUFFER + 1, sizeof( char ) );
    	
    	printf( "Write anything: " ); 
    	fgets( input, MAX_BUFFER, stdin );
    	
    	printf( "You wrote: %s \n" , input );
    	
    	p = strchr( input, 'm' );
    	while( p != NULL )
    	{
    		o = p;
    		*p = 'S';
    		p = strstr( o + 1, "m" );
    	}
    	
    	printf( "I changed: %s \n", input );
    
    	free( input );
    	return 0;
    }

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,632
    Quote Originally Posted by bvkim
    this is better version:
    No, it is worse. You are over-complicating the problem by demonstrating dynamic memory allocation where it is unnecessary. If you want to demonstrate the use of strchr(), demonstrate the use of strchr() (plus the use of strstr() and the additional pointer o is unnecessary). But as far as I am concerned, you should not demonstrate the use of strchr() until it is clear that neo28 is able to get from his/her current code to a solution.
    Last edited by laserlight; 05-15-2009 at 07:59 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    May 2009
    Posts
    6
    Quote Originally Posted by laserlight View Post
    No, it is worse. You are over-complicating the problem by demonstrating dynamic memory allocation where it is unnecessary. If you want to demonstrate the use of strchr(), demonstrate the use of strchr(). But as far as I am concerned, you should not demonstrate the use of strchr() until it is clear that neo28 is able to get from his/her current code to a solution.
    Sorry guys getting a tad confused now with all the suggestions ! thank you for all the help though

    this is what ive done so far, dont think its in the right place though and what I need to get rid of

    Code:
    #include <string.h>
    
    int main( void )
    {
       char text[ 256 ] = { '\0' };
       int found = 0;
       int counter;
    
       printf( "Please enter a string: " );
       scanf( "%255s", text );
       
       for ( counter = 0; counter < strlen( text ); counter++ )
       {
          if ( text[ counter ] == 'm' )
       {
             found = 1;
             break;
          }   
       }
       
       if ( found )
         if ( text[ counter ] == 's' )
          printf( "\n%s", text );
       else
          printf( "\nNot found" );
    }

  13. #13
    Registered User
    Join Date
    May 2009
    Posts
    60
    Quote Originally Posted by laserlight View Post
    No, it is worse. You are over-complicating the problem by demonstrating dynamic memory allocation where it is unnecessary. If you want to demonstrate the use of strchr(), demonstrate the use of strchr() (plus the use of strstr() and the additional pointer o is unnecessary). But as far as I am concerned, you should not demonstrate the use of strchr() until it is clear that neo28 is able to get from his/her current code to a solution.
    could u fix my code w/o using pointer `o` ?

  14. #14
    Registered User
    Join Date
    May 2009
    Posts
    6
    Actually just done this

    Code:
    #include <string.h>
    
    int main( void )
    {
       char text[ 256 ] = { '\0' };
       int found = 0;
       int counter;
    
       printf( "Please enter a string: " );
       scanf( "%255s", text );
       
       for ( counter = 0; counter < strlen( text ); counter++ )
       {
          if ( text[ counter ] == 'm' )
             (  text[ counter ] = 's' );
       {
             found = 1;
             break;
          }   
       }
       
       if ( found )
         if ( text[ counter ] == 's' )
          printf( "\n%s", text );
       else
          printf( "\nNot found" );
    }
    and it seems to work, yet will only work for one word and anything else the user types it wont change


    Cheers

    Craig

  15. #15
    Registered User
    Join Date
    May 2009
    Posts
    60
    @neo28:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main( void )
    {
       char text[ 256 ] = { '\0' };
       int found = 0;
       int counter;
    
       printf( "Please enter a string: " );
       scanf( "%255s", text );
       
       for ( counter = 0; counter < strlen( text ); counter++ )
       {
          if ( text[ counter ] == 'm' )
       	  {
             found = 1;
             break;
          }   
       }
       
       if ( found )
       {
          if ( text[ counter ] == 'm' )
          	 text[ counter ] = 'S';
         	 printf( "%s\n", text );
       }
       else
          printf( "\nNot found" );
          
       return 0;
    }

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem using java programs within C code
    By lemania in forum Linux Programming
    Replies: 1
    Last Post: 05-08-2005, 02:02 AM
  2. problem with some simple code =/
    By Josh Norton in forum C++ Programming
    Replies: 3
    Last Post: 02-23-2004, 05:27 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. Simple Compile Time Problem - HELP!
    By kamikazeecows in forum Windows Programming
    Replies: 2
    Last Post: 12-02-2001, 12:30 PM
  5. Big Code, Little Problem
    By CodeMonkey in forum Windows Programming
    Replies: 4
    Last Post: 10-03-2001, 05:14 PM

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