![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: May 2006
Posts: 1
Rep Power: 0
![]() |
strlen help...
Hi,
Im having a problem with an assignment...Im supposed to format a string...and this is doing my head in... I have to use a for loop, use strlen to control which character to format...the thing is, the first character in the string MUST be uppercase and the rest lowercase, and naturally, we have to use toupper and tolower... If you could provide even an example of how this would be implemented then I would be very happy... Thanks in advance |
|
|
|
|
|
#2 |
|
Professional Programmer
|
__________________
The world's first athletic computer geek! The home of PrProgramsStudios How not to post a question: <-- Please don't reply Last edited by Prm753; May 31st, 2006 at 12:42 PM. |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4
![]() |
This capitalizes all of the words in a string.
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
char *capitalize(char *dest)
{
char *p=dest;
char last=0;
for(;*p;p++)
{
*p=tolower(*p);
if( isalpha((int)*p) && isspace(last)|| last ==0)
{
*p=toupper(*p);
}
last=*p;
}
return dest;
}
int main()
{
char test[32]={0x0};
char *p=NULL;
strcpy(test,"HELLO how are u?");
p=capitalize(test);
printf("%s\n",p);
return 0;
}Sorry no strlen()... |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Oct 2005
Location: Melbourne, Australia
Posts: 126
Rep Power: 4
![]() |
Some pseudo code for you to ponder:
For i = 0, while i is less then the length of the string, increment i by 1 per loop. If i is 0, change the letter to uppercase, otherwise change the letter to lowercase. Without writing the actual code for you, that should get you well on your way.
__________________
it's ironic considerate rarity patron of love higher knowledge engulfs me... |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|