Thread: toupper command

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    42

    toupper command

    Hello everyone,

    I am working on a problem and I am not sure of the syntax for the toupper command. What I am trying to solve is when a user enters a string of characters C goes out and changes them to all upper case.

    Here is what I have so far using a char array and gets and puts commands just not sure in the puts command how you integrate the toupper.

    Thanks in advance

    DMKanz07

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    /* Function main begins program execution */ 
    int main()
    {
        /* Create char array */
        char s [ 100 ];
        
        printf( "Enter a line of text: " );
        
        /* Use gets to read line of text */
        gets( s );
        
        printf( "\n" );
        
        printf( "The line printed in UPPER case: " );
        puts ( toupper ( s ) );
        
        printf( "\n" );
        
        /* Indicates successful termination */
        return 0;
        
    } /* End Main */
    Last edited by dmkanz07; 04-17-2007 at 08:21 AM.

  2. #2
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Why not to use gets.

    As for the toupper stuff, the function takes in a char and returns its upper-case equivalent.

    So if you'd want to convert a string to all upper case, you'd have to loop through all its chars and send them through toupper.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    42
    Does toupper only convert one character at a time?

    Thanks

    DMKanz07

  4. #4
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Yes :

    toupper.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  5. #5
    Registered User divineleft's Avatar
    Join Date
    Jul 2006
    Posts
    158
    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    
    /* Function main begins program execution */
    int main()
    {
            /* Create char array */
            char s [ 100 ];
            int i;
            printf( "Enter a line of text: " );
    
            /* Use gets to read line of text */
            gets( s );
            printf( "\n" );
    
            /* Loop through the array and change every character
             * to its uppercase equivilant */
            for(i = 0; i < strlen(s); ++i)
            {
                s[i] = toupper(s[i]);
            }
    
            printf( "The line printed in UPPER case: " );
            puts (s);
    
            printf( "\n" );
    
            /* Indicates successful termination */
            return 0;
    
    } /* End Main */
    although i would recommend using fgets instead of gets.

  6. #6
    Registered User
    Join Date
    Mar 2007
    Posts
    42
    What is the difference between gets and fgets? I found examples of fgetc but not fgets ( Deitel C how to program ).

    DMKanz07

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    fgets() allows you to specify string length, gets() does not. As such, there is the risk that the buffer will be overrun when using gets().
    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

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    This is a bad idea:
    Code:
    for(i = 0; i < strlen(s); ++i)
    strlen() is rather expensive to execute, so you should save the result.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. problem with "touch" command in c program
    By Moony in forum C Programming
    Replies: 10
    Last Post: 08-01-2006, 09:56 AM
  3. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  4. Ping problem
    By bladerunner627 in forum C++ Programming
    Replies: 12
    Last Post: 02-02-2005, 12:54 PM
  5. exe files in -c- language
    By enjoy in forum C Programming
    Replies: 6
    Last Post: 05-18-2004, 04:36 PM