#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    3
    Rep Power
    0

    Converting a string to uppercase


    Hello everybody I am trying to convert a lowercase string to uppercase by using C programming. I have managed to convert only the first letter of the string into uppercase, but that's it, only one character and I have a string that must be converted into uppercase characters. This is a school assignment and I have the full intention of learning the material but because it's a summer class, everything is going very fast and I am not understanding very much. If somebody could give me an explanation of how this code is working and how to fix it, I will appreciate it very much. The string is: hello, i am a student.

    #include <stdio.h>

    int main()
    {
    char lowercase, uppercase;
    printf("type a lowercase character: ");
    scanf("%c", &lowercase);

    if(lowercase>='a' && lowercase<= 'z')
    {
    uppercase = ('A' + lowercase - 'a');
    }
    else
    uppercase = lowercase;
    printf("the uppercase character is %c", uppercase);

    return 0;
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,089
    Rep Power
    2222
    So iterate through the rest of the string and repeat the operation. A string ends with a null-terminator, which is '\0' (ie, a character whose ASCII code is zero).
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    3
    Rep Power
    0
    Originally Posted by dwise1_aol
    So iterate through the rest of the string and repeat the operation. A string ends with a null-terminator, which is '\0' (ie, a character whose ASCII code is zero).
    I am strongly confused at the moment, but thank you for replying. I appreciate your help!
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,703
    Rep Power
    480
    You'll need a way to store and initialize a string. One way is

    char*s = "hello starving student.";

    There are other ways.


    And you'll need a way to iterate through the string. Iterations usually have starting and termination conditions. According to dwise1_aol you could iterate over each character of the string and finish when the character has the value 0. That's ASCII nul, look it up at http://www.asciitable.com/

    Now, a word about a good approach for this project. Write the function translit. It will be more flexible, more used.

    char*translit(char*array, char*from, char*to);

    Such that the code fragment

    char s[] = "hello, I'm a student";
    translit(s, "a-z", "A-Z");
    puts(s);

    writes onto stdout:
    HELLO, I'M A STUDENT

    To some extent, translit should just follow the rules of the unix tr program. With a little cleverness you can specify appropriate rules for the "from" and "to" sets for the squeeze, delete, and complement options of tr without needing to pass them as a separate options argument to translit.
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    3
    Rep Power
    0
    Thank you for the assistance b49P23TIvg. As of now, we have not learned about arrays, but I will definitely benefit from your explanation in a couple of days when the professor covers arrays. This assignment was intended to be completed using basic knowledge about C programming such as variables, if statements and loops. Loops gave me some trouble, but I am going to review it and hopefully I will understand better your explanation.

IMN logo majestic logo threadwatch logo seochat tools logo