C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28691
Number of posts: 94711

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
turbo c Posted by Dandirom on 16 Mar 2009 at 12:31 AM
Hi, Can anyone point me to a post or tutorial that would help with answering this problem? No answers needed -- just pointers that would help point to the right direction to solving this. Thanks.


Prompt the user for full name. Keep on asking until all characters are valid. Then display full name with first letters capital.
Result should be something like this:


Enter your name: juan dela 123 cruz...
Name has invalid characters!
Enter your name: jUan dEla CrUx...
Your name is: Juan Dela Cruz
Report
Re: turbo c Posted by Lundin on 16 Mar 2009 at 12:44 AM
After taking the input from the user, you need a for loop and the functions toupper() and tolower() from ctype.h.
Report
Re: turbo c Posted by Dandirom on 16 Mar 2009 at 1:23 AM
: After taking the input from the user, you need a for loop and the
: functions toupper() and tolower() from ctype.h.

Thank you. I got that idea but can you point me to a forum or tutorial that shows how to use them and gives an outline of the structure? Sorry to bother you with this trivial problem -- I've just been trying to find a tutorial that covers this.
Report
Re: turbo c Posted by Lundin on 16 Mar 2009 at 1:35 AM
Google "toupper example":

http://www.google.com/search?hl=sv&q=toupper+example&btnG=Google-s%C3%B6kning&meta=&aq=f&oq=
Report
Re: turbo c Posted by Dandirom on 16 Mar 2009 at 1:43 AM
Thank you very much!
Report
Re: turbo c Posted by Dandirom on 16 Mar 2009 at 6:06 AM
Where can I search for forums about this?

Report
Re: turbo c Posted by Lundin on 16 Mar 2009 at 6:58 AM
The easy and perhaps best way is to loop through the string and manually check every letter.

The hard, fancy and not particulary efficient way is to use the strtok() function (string tokenizer). I'd stay away from that one if you are new to string handling.
Report
Re: turbo c Posted by Dandirom on 16 Mar 2009 at 7:04 AM
Thank you again. You have to forgive an this amateur who's trying to learn the ropes.
Report
Re: turbo c Posted by Dandirom on 16 Mar 2009 at 7:21 AM

main()
{
int x;
char name[30];

clrscr();

puts("enter your name: ");
gets(name);

for(x=0;x<strlen(name);x++)
{
if(isalpha(name[x])==0 && isspace(name[x])==0)
{
puts("Name contains invalid character!\nEnter name: ");
gets(name);
}

}

printf("\nYour name is:\n");
printf("%c", toupper(name[0]));

for(x=1; x<strlen(name); x++)
{
if(isupper(name[x])!=0)
{
printf("%c", tolower(name[x]));
}
else if(isspace(name[x])!=0)
{
printf(" %c", toupper(name[x+1]));
x+=1;
}
else
{
printf("%c", name[x]);
}
}

getch();
}
Report
Re: turbo c Posted by Lundin on 16 Mar 2009 at 7:42 AM
Some comments:


: main()

This isn't valid C. It should be int main().


: puts("enter your name: ");
: gets(name);

This will only read the string until it encounters a space. That is, it will only read the forename. fgets() might be a better function if you wish to read several strings and store them in one array.


: for(x=0;x<strlen(name);x++)

A hint for optimizing: never use strlen() inside the loop condition, or it will be executed for every run of the loop. Your program will be much faster if you write like this:

int length;
...

length = strlen(name);
for(x=0; x<length; x++)


: if(isalpha(name[x])==0 && isspace(name[x])==0)

This should work, but note that the names will contain spaces.


: else if(isspace(name[x])!=0)
: {
: printf(" %c", toupper(name[x+1]));
: x+=1;

This will likely cause trouble. If you run into a space, you will want to print the space. Otherwise the names will appear like "BillGates".


Also, avoid clrscr() and getch() when possible, since they aren't standard C functions.
Report
Re: turbo c Posted by Dandirom on 16 Mar 2009 at 11:02 PM
That was great! Thank you very much for your input.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.