Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Help in upgrading my code that converts a lower-case string to upper c

  1. May 1, 2013 #1
    hello
    I'm a beginner in c programming. I wrote a code that converts the lower case that the user entered to upper case characters.
    I want to improve my program , but since i'm a beginner i might not be aware of some technics .
    my program can only allow you to enter as many characters as you want but without using space (one word ) .
    1. how can i allow the user to enter more than one word ?
    2. when i say char letter[20]; what does the 20 mean , because i entered more than 20 characters and it worked fine .


    the code is
    Code (Text):
    #include <stdio.h>
    #include <string.h>
    int main (void) {

    char letter[20];
    printf("please enter lower-case character without spaces and then press enter to get them in upper case:");
    scanf("%s",letter);
    int nameLength = strlen(letter);
       int i;
        printf("\nthe upper case characters of your entry is : \n");
       for (i=0;i<nameLength;i++) {
            int u=letter[i]-32;
           printf("%c",u);
           
       }
        return 0;
    }
     


    what I get is

    Here I tried checking if the entry was a lower case , but that didnt work even if I enter an upper case it will print -32 form it

    Code (Text):
    #include <stdio.h>
    #include <string.h>
    int main (void) {

    char letter[20];
    printf("please enter lower-case character without spaces and then press enter to get them in upper case:");
    scanf("%s",letter);
    int nameLength = strlen(letter);
       int i;
        printf("\nthe upper case characters of your entry is :");
        int u;
        if ( u > 96 )  {
     for (i=0;i<nameLength;i++) {
            int u=letter[i]-32;
           printf("%c",u);
           
       }
    }
    else {
     printf("invalid input");
    }
       
       
        return 0;
    }
     
     
    Last edited: May 1, 2013
  2. jcsd
  3. May 1, 2013 #2

    rcgldr

    User Avatar
    Homework Helper

    Check to make sure that the input character is within the range of a lower case letter.

    An alternate method would be to create a 256 byte table where the value in the table equals the index into the table, except for lower case letters, in which case the value in the table is 32 less than the index, so that those entries in the table will convert lower case to uppper case. This method can also be used for other conversion, such as indexing a 16 byte table with an index ranging from 0 to 15 and the content of the table containing an ASCII characer corresponding to a hex digit (0, 1, 2, ..., 9, A, B, C, D, E, F).

    You could create the table at the start of your program, or use another program to create the table as a text file, then import that text file of the table into the source code of program that will use the table.
     
  4. May 1, 2013 #3
    To do this, you need to use a loop. Your program enters the loop, asks the user to enter a word and prints the word in uppercase. You could make it, so that if they enter the word "quit", the the loop stops and the program terminates, or you could ask the user if they want to enter another word, there are many ways to do this. Use a while() loop.

    The 20 means that your array of char has 20 "slots" that will fit a char. So you are limited to storing 20 chars. The reason it "works" with more than 20 chars is because, luckily, you haven't got much going on in your program, and the memory after your 20 chars wasn't being used yet, so your program just prints what it finds. Programming in C means that you have to be PAINFULLY AWARE of how much memory you are allocating for each of your variables. You should always, always, ALWAYS check your inputs to make sure that they are of a suitable size for the variable in which you want to store them. Bad things happen!

    As for your uppercase conversion, I assume you are familiar with the ASCII table? It looks like you are because you are attempting to check the integer value of your chars, but the logic in your 2nd piece of code isn't sensible. Remember, you have to check whether each character in the string is lowercase, and if it is, you need to print it as uppercase. Your if() statement should be inside the for() loop, checking each character, and you should have an else clause that takes care of the case when a character is already in uppercase.

    Before you write your code, it's a very good idea to flesh out your algorithm on a piece of paper with a flow chart. This is a very short and simple algorithm, and I encourage you to take a few minutes to draw it up and make sure you understand what needs to happen, and in what order.
     
    Last edited: May 2, 2013
  5. May 26, 2013 #4

    harborsparrow

    User Avatar
    Gold Member

    Is this homework? Sure looks like it.
     
  6. May 26, 2013 #5
    no it was not :)
    the homw work was to use a delay function , this was something I wanted to know for future programs
     
  7. May 26, 2013 #6

    harborsparrow

    User Avatar
    Gold Member

    Good!

     
  8. May 26, 2013 #7

    Borek

    User Avatar

    Staff: Mentor

    Beware: better declare explicitly if you are using signed or unsigned characters in your table. Otherwise what works on your computer may stop to work when compiled elsewhere.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Help in upgrading my code that converts a lower-case string to upper c
  1. Help Convert Code (Replies: 0)

  2. Critique my C++ code (Replies: 11)

Loading...