June 26th, 2013, 03:06 AM
-
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;
June 26th, 2013, 09:15 AM
-
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).
June 26th, 2013, 08:07 PM
-
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!
June 26th, 2013, 10:09 PM
-
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!
June 27th, 2013, 11:35 PM
-
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.