Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C (http://www.programmingforums.org/forum60.html)
-   -   strlen help... (http://www.programmingforums.org/showthread.php?t=10103)

The_Razgriz_Ace May 31st, 2006 12:26 PM

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

Prm753 May 31st, 2006 12:31 PM

Emazing! First Google Hit!
Cprogramming.com reference

jim mcnamara May 31st, 2006 6:11 PM

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()...

bivhitscar Jun 1st, 2006 2:43 AM

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.


All times are GMT -5. The time now is 8:00 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC