C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old April 13th, 2003, 04:41 PM
kmr6655 kmr6655 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 7 kmr6655 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question Character Array and Pointer

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));

}

}

Reply With Quote
  #2  
Old April 13th, 2003, 05:08 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,432 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 4 Weeks 1 Day 22 h 29 m 51 sec
Reputation Power: 784
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.

Reply With Quote
  #3  
Old April 13th, 2003, 06:58 PM
kmr6655 kmr6655 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 7 kmr6655 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thumbs up

Thanks for your help. Yes, I am new to programming. I hate to be so obvious! Thanks for the tip, I appreciate it.

Kathy

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Character Array and Pointer


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway