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; Originally Posted by bvkim could u fix my code w/o using pointer `o` ? You only use o in one ...

  1. #16
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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
    21,313
    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'.
    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. #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
    21,313
    bvkim, let people do their own homework.
    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. #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
    21,313
    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.
    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

Page 2 of 2 FirstFirst 12
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