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

In summary, this conversation was about a beginner in C programming who wrote a code to convert lower case characters to upper case, but wanted to improve their program. They asked how to allow the user to enter more than one word and what the number in the char letter[20] declaration meant. Suggestions were given to use a loop for multiple words and to check the size of inputs to avoid errors. It was also recommended to create a table for more efficient conversion. The conversation concluded with a reminder to carefully plan algorithms and to be explicit in declaring signed or unsigned characters.
  • #1
blue_tiger30
29
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
  • #2
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.
 
  • #3
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:
  • #4
Is this homework? Sure looks like it.
 
  • #5
no it was not :)
the homw work was to use a delay function , this was something I wanted to know for future programs
 
  • #6
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
 
  • #7
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.
 

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

1. How do I convert a lower-case string to upper case in my code?

To convert a lower-case string to upper case, you can use the built-in string method toUpperCase(). This method will return a new string with all characters converted to upper case. For example, if your string variable is str, you can use str.toUpperCase() to convert it to upper case.

2. Can I convert only certain characters to upper case in my string?

Yes, you can use the replace() method to replace specific characters in your string with their upper-case versions. For example, if you want to convert all vowels in your string to upper case, you can use str.replace(/[aeiou]/g, function(match){ return match.toUpperCase(); }). This will replace all vowels with their upper-case versions in your string.

3. How can I handle special characters during the conversion process?

If your string contains special characters, you may need to use the normalize() method before converting it to upper case. This method will convert any special characters to their standard form, which can then be converted to upper case using toUpperCase(). For example, str.normalize().toUpperCase() will first normalize your string and then convert it to upper case.

4. Can I convert a string to upper case without affecting the original string?

Yes, you can use the slice() method to create a copy of your original string and then convert the new string to upper case. This will prevent any changes to the original string. For example, str.slice().toUpperCase() will create a copy of str and then convert it to upper case.

5. Is there a way to convert a lower-case string to upper case using a loop?

Yes, you can use a for loop to iterate through the characters in your string and convert them to upper case one by one. However, this method may not be as efficient as using the built-in string methods. For example, you can use the following loop to convert a string variable str to upper case: for(var i = 0; i < str.length; i++) { str[i] = str[i].toUpperCase(); }

Similar threads

  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
4
Views
760
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
20
Views
2K
  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
4
Views
915
  • Programming and Computer Science
Replies
30
Views
2K
  • Programming and Computer Science
Replies
5
Views
915
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
1
Views
981
Back
Top