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

  • Thread starter Thread starter blue_tiger30
  • Start date Start date
  • Tags Tags
    Code String
AI Thread Summary
The discussion centers on improving a C program that converts lowercase strings to uppercase. Key points include allowing multi-word input by using a loop and clarifying that the array size (e.g., char letter[20]) limits the number of characters stored, which can lead to undefined behavior if exceeded. The logic for checking if characters are lowercase before conversion needs to be refined, with the condition placed inside the loop to evaluate each character. Additionally, creating a lookup table for character conversion is suggested as an alternative method. Understanding memory allocation and proper input validation is emphasized as crucial for effective programming in C.
blue_tiger30
Messages
28
Reaction score
0
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:
#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
please enter lower-case character without spaces and then press enter to get them in upper case:hello there

the upper case characters of your entry is :
HELLO
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:
#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;
}

please enter lower-case character without spaces and then press enter to get them in upper case:DDDDD

the upper case characters of your entry is :$$$$$
 
Last edited:
Technology news on Phys.org
Check to make sure that the input character is within the range of a lower case letter.

blue_tiger30 said:
I want to improve my program , but since I'm a beginner i might not be aware of some technics.

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.
 
blue_tiger30 said:
1. how can i allow the user to enter more than one word ?

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.

blue_tiger30 said:
2. when i say char letter[20]; what does the 20 mean , because i entered more than 20 characters and it worked fine .

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:
Is this homework? Sure looks like it.
 
no it was not :)
the homw work was to use a delay function , this was something I wanted to know for future programs
 
Good!

blue_tiger30 said:
no it was not :)
the homw work was to use a delay function , this was something I wanted to know for future programs
 
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.
 
Back
Top