Thread: Simple C code problem

  1. #16
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by bvkim View Post
    could u fix my code w/o using pointer `o` ?
    You only use o in one place, and it could easily be changed to be "p".

  2. #17
    Registered User
    Join Date
    May 2009
    Posts
    60
    Quote Originally Posted by tabstop View Post
    You only use o in one place, and it could easily be changed to be "p".
    my bad, i mis-understood while reading strchr() definition.

    i'm clear now!

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by neo28
    and it seems to work, yet will only work for one word and anything else the user types it wont change
    The reason is that you are only reading in one "word" with the %s format specifier. You could switch to use the %c format specifier, but it may be easier to just use fgets() as bvkim demonstrated.

    Why do you have the found variable? You do not need it, and your attempts to use it seem to be confusing you. For example, you are still breaking from the loop after finding the first 'm'.
    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

  4. #19
    Registered User
    Join Date
    May 2009
    Posts
    60
    well then, here the completed version following neo28 demand:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define MAX_BUFFER	256
    
    int main( void )
    {
       char text[ MAX_BUFFER ] = { '\0' };
    
       int counter = 0;
    
       printf( "Please enter a string: " );
       fgets( text, MAX_BUFFER, stdin );
    	
       for( ; counter < strlen( text ); ++counter )
       {
       		if( text[ counter ] == 'm' )
       			text[ counter ] = 'S';
       }
       
       printf( "Modified: %s\n", text );
          
       return 0;
    }

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    bvkim, let people do their own homework.
    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. #21
    Registered User
    Join Date
    May 2009
    Posts
    60
    Quote Originally Posted by laserlight View Post
    bvkim, let people do their own homework.
    oops ! sorry, I've never thought of people asking for homework here...my bad !

  7. #22
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by bvkim View Post
    oops ! sorry, I've never thought of people asking for homework here...my bad !
    You've only just signed up, eh? It happens several times a week - in more or less subtle/direct/desperate ways.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #23
    Registered User
    Join Date
    May 2009
    Posts
    6
    Hey guys,

    Thanks for all the help, It wasn't homework, I'm just trying to learn C in general and found I learn more by trying to write a program and edit it to do simple things than read through books all the time !

  9. #24
    Registered User
    Join Date
    Apr 2009
    Posts
    41
    Most schools have tutor assistance. I realy don't understand why they don't use them. Just the over-all laziness?
    And truely it hurts none to give simple solutions to simple problems. A beginner programmer really needs a base in which to build upon. So ramming their heads against the wall over something simple isn't really correct. Its when they come back with nearly the same problems and still don't know how to do them, you should ram them in the head.
    Last edited by strickyc; 05-16-2009 at 09:51 AM.

  10. #25
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by strickyc
    And truely it hurts none to give simple solutions to simple problems. A beginner programmer really needs a base in which to build upon. So ramming their heads against the wall over something simple isn't really correct. Its when they come back with nearly the same problems and still don't know how to do them, you should ram them in the head.
    Yes, it is usually fine to give a "model answer" after you judge that the question asker has tried hard enough, or to give a working example that does not directly answer the question. In this case, neo28 was so close to a correct solution that giving one takes away from neo28's satisfaction of solving the problem himself/herself when it is just within reach.
    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

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, 06:27 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Simple Compile Time Problem - HELP!
    By kamikazeecows in forum Windows Programming
    Replies: 2
    Last Post: 12-02-2001, 01:30 PM
  5. Big Code, Little Problem
    By CodeMonkey in forum Windows Programming
    Replies: 4
    Last Post: 10-03-2001, 05:14 PM