Making a char array uppercase

This is a discussion on Making a char array uppercase within the C Programming forums, part of the General Programming Boards category; Whats up... Code: int a, b; char text[25]; char upper[25]; for (a = 0, b = strlen(text); a < b; ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    83

    Making a char array uppercase

    Whats up...

    Code:
        int a, b;
        char text[25];
        char upper[25];
            
        for (a = 0, b = strlen(text); a < b; a++)
        {
            if (isalpha(word[a]))
                upper[a] = toupper(text[a]);
            else
                upper[a] = text[a];
        }
    This code works in converting the word to uppercase, but it often puts garbage characters at the end of the converted word (upper). Is this occurring because text is not the full length of the array and so upper still displays those chars? How would I fix this?
    Thank you all

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Not so much that text is not the full length of the array, but that text does not have a null terminator '\0' where it should.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    83
    How do I put that in?

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by pollypocket4eva View Post
    How do I put that in?
    After the for loop, a will still represent the next character in text, so:

    Code:
    }     // end of for loop
    text[a]='\0'
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    After the loop terminates, of course.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    Registered User
    Join Date
    Nov 2008
    Posts
    83
    Yes --- thank you!

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
            if (isalpha(word[a]))
                upper[a] = toupper(text[a]);
            else
                upper[a] = text[a];
    is unnecessarily complicated. toupper is only going to change things that are different between upper and lower case, which is (generally speaking) only alpha characters. So the if-statement isn't needed - just call toupper on every character in the string. It will be about the same speed as isalpha + upper[a] = text[a], but shorter and simpler to read.

    Also, if you wish (I'm not at all saying you should, but it's good to recognize that it's an alternative), set b to strlen(text)+1, and you would automatically copy the zero that terminates the string.

    --
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  2. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  3. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

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