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
Click For Summary

Discussion Overview

The discussion revolves around improving a C programming code that converts lower-case strings to upper-case characters. Participants explore various aspects of the code, including user input handling, memory allocation, and character conversion techniques. The conversation includes both conceptual and technical clarifications.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant seeks advice on allowing multi-word input and clarifies the meaning of the array size in their code.
  • Another participant suggests checking if the input character is within the range of lower-case letters and proposes using a 256-byte table for character conversion.
  • A different participant recommends using a loop to handle multiple words and emphasizes the importance of memory allocation awareness in C programming.
  • Concerns are raised about the logic in the participant's code for checking character cases, suggesting that checks should be performed within a loop for each character.
  • One participant questions whether the original post is related to homework, while another clarifies that it is not, mentioning a different homework assignment.
  • A warning is issued regarding the declaration of signed or unsigned characters in the proposed table, highlighting potential portability issues across different systems.

Areas of Agreement / Disagreement

Participants express varying opinions on coding practices and techniques, with no consensus reached on the best approach to improve the original code. Multiple competing views remain regarding input handling and character conversion methods.

Contextual Notes

Participants discuss the implications of memory allocation and input validation, noting that the original code may lead to undefined behavior if the input exceeds the allocated array size. There are also unresolved questions about the logic for character case checking.

Who May Find This Useful

Beginners in C programming, particularly those interested in string manipulation and character conversion techniques, may find this discussion beneficial.

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.
 

Similar threads

Replies
7
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 20 ·
Replies
20
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 6 ·
Replies
6
Views
3K
Replies
14
Views
4K
Replies
5
Views
2K
  • · Replies 6 ·
Replies
6
Views
6K
  • · Replies 30 ·
2
Replies
30
Views
5K