|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
|||
|
|||
|
Hi All...
Hoping someone can help me with what is probably a silly question. I am to take user input, put it into uppercase, then lowercase. The code below does output the uppercase, except I am missing the first character. I don't get any output for the lowercase. Thanks in advance..any help is appreciated! #include <stdio.h> #include <ctype.h> void main (void) { char text[70]; char *text_ptr=text; int i; printf ("\nEnter a line of text (up to 69 [COLOR=blue]characters):\n"); gets(text); text[70]='\0';> printf("\nThe line of text in uppercase [COLOR=blue]is:\n\n"); i=0; while(*text_ptr) { *text_ptr++; putchar(toupper(*text_ptr)); } printf("\n\nThe line of text in lowercase is:\n\n"); i=0; while(*text_ptr) { *text_ptr++; putchar(tolower(*text_ptr)); } } |
|
#2
|
||||
|
||||
|
For printing in uppercase, flip the order of the two statements in the loop, (i.e.) uppercasing the pointer and then increment it. You're incrementing the pointer first and then uppercasing, which is why you're missing the first char:
Code:
while(*text_ptr)
{
putchar(toupper(*text_ptr));
text_ptr++;
}
As for the lowercasing part, you forgot to reset your pointer to the beginning of the string again. As before, also change the order of your statements inside the loop and you should be good to go. Code:
printf("\n\nThe line of text in lowercase is:\n\n");
text_ptr = text; /* <--- Point text_ptr to the beginning of the string again */
while(*text_ptr)
{
putchar(tolower(*text_ptr));
text_ptr++;
}
I realize that you're probably a beginner to C programming, so I'll give you a little friendly advice. Using gets() is highly discouraged for any serious programming. Consider using fgets() or fread() instead. See the BUGS section in http://www.openbsd.org/cgi-bin/man....386&format=html for why. Last edited by Scorpions4ever : April 13th, 2003 at 05:11 PM. |
|
#3
|
|||
|
|||
|
Thanks for your help. Yes, I am new to programming. I hate to be so obvious! Thanks for the tip, I appreciate it.
Kathy |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > Character Array and Pointer |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|