NEED HELP! make first letter of each word in user input text uppercase

This is a discussion on NEED HELP! make first letter of each word in user input text uppercase within the C Programming forums, part of the General Programming Boards category; Hello, I am in need of some major help. I need to write a program to make the user input ...

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    23

    Angry NEED HELP! make first letter of each word in user input text uppercase

    Hello,
    I am in need of some major help.
    I need to write a program to make the user input some text,
    and the first letter of each word has to be uppercase.
    (have to use while loops)

    So for example lets say the user inputs:
    i lOvE pRoGrAmMiNg
    The output needs to be:
    I Love Programming

    Code:
    int main()
    {
        char c, lower_c;
    
        printf("Enter Text\n");
    
        while (c != '\n' && c >= 0)
        {
            if (c >= 'A' && c <= 'Z')
                lower_c = c + 32;
            else
                lower_c = c;
    
            putchar(lower_c);
    
            c = getchar();
        }
        
        
        
        putchar('\n');
        return 0;
    }
    I have started this code by making the letters lowercase (I don't know if this was the right way to approach this, but I am hoping you can help me with that). I am not sure how proceed after this step, the step of making the first letter uppercase.
    If anybody could help me out with this, it would be much appreciated.
    Thank you for all your help in advance.
    (PS for the program, loops should be while loops)
    Last edited by tshort82392; 09-22-2013 at 01:11 AM.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,450
    tolower - C++ Reference - to make all lower case

    isspace - C++ Reference - to find space
    isalpha - C++ Reference - to find where word begins after space
    toupper - C++ Reference - to make first letter in the word uppercase
    Last edited by vart; 09-22-2013 at 04:33 AM.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Sep 2013
    Posts
    13
    Code:
    #include <stdio.h>
    
    char *firstLetterToUpper(char *);
    int strlen(char *);
    void upper(char *);
    void lower(char *);
    
    
    int main(void)
    {
        char data[64];
    
    
        printf("Enter some text:\n");
    
    
        if (fgets(data, sizeof data, stdin)) {
            firstLetterToUpper(data);
        } else {
            return 1;
        }
    
    
        printf("%s", data);
        
        return 0;
    }
    
    
    char *firstLetterToUpper(char *str)
    {
        upper(str++);
    
    
        while (*str++ != '\0')
            if (*str == ' ') {
                upper(++str);
            } else {
                lower(str);
            }
    }
    
    
    void upper(char *c)
    {
        if (*c >= 'a' && *c <= 'z')
            *c = *c + 'A' - 'a';
    }
    
    
    void lower(char *c)
    {
        if (*c >= 'A' && *c <= 'Z')
            *c = *c + 'a' - 'A';
    }
    
    
    int strlen(char *s)
    {
        char *p = s;
    
    
        while (*p++ != '\0')
            ;
        return p - s;
    }
    Last edited by riemann; 09-22-2013 at 06:34 AM.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,157
    rieman, it is actually strongly discouraged here to simply give code asked for. People learn better if they work out something for themselves, rather than being spoon-fed.

    And your code is not necessarily correct anyway - it doesn't work with all character sets.

    You could have used standard library functions (linked to by vart) to simplify your code considerably - and have greater chance of it being correct. And strlen() is a standard function, so no need to roll your own.
    Last edited by grumpy; 09-22-2013 at 06:58 AM.
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    Registered User
    Join Date
    Sep 2013
    Posts
    13
    Yes, sorry I forgot to mention that my code only works with ASCII.
    The reason I rolled my own functions was to let the op understand what exactly they're doing so it was more clear instead of just using the standard library functions..

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The very first letter the user enters, must be uppercase. (You should #include <ctype.h>, as well as stdio.h)

    Every first letter AFTER a space, must be uppercase. You can use isspace() or if(stringFromUser[i] == ' ') if you like. Same idea for a punctuation like comma, period, or newline: '\n'. At the end of a line, there will be no space, but the next line is likely to start with a new word, and require an uppercase letter.



    Give that a try.
    Last edited by Adak; 09-22-2013 at 03:04 PM.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,157
    Quote Originally Posted by Adak View Post
    At the end of a line, there will be no space, but the next line is likely to start with a new word, and require an uppercase letter.
    The end of a line will typically be marked by a '\n'. isspace('\n') is non-zero (i.e. true).

    There is nothing preventing a user from entering multiple spaces (or other whitespace characters) and/or punctuation characters between words. ispunct(), another function declared in <ctype.h>, can help detect punctuation characters.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 11-24-2012, 03:10 AM
  2. Replies: 3
    Last Post: 10-12-2010, 01:40 PM
  3. make a char display a word or letter
    By dyelax in forum C++ Programming
    Replies: 12
    Last Post: 10-13-2009, 11:55 AM
  4. How do I make it wait for user input
    By earth_angel in forum Windows Programming
    Replies: 1
    Last Post: 06-29-2005, 01:34 PM
  5. check if user input matches a word
    By fakebloo in forum C++ Programming
    Replies: 1
    Last Post: 12-05-2004, 06:12 PM

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