more capitalize char help needed

This is a discussion on more capitalize char help needed within the C++ Programming forums, part of the General Programming Boards category; ok i can understand the reply to my earlier question on how to make only the first char of a ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    3

    Question more capitalize char help needed

    ok i can understand the reply to my earlier question on how to make only the first char of a string into a capital and leave the rest small

    the next thing confusing me is if i have a sentence of words with one space between each word, how would i get the first letter of each word to capitalize while leaving the other letters small?

    thanks again newbie

  2. #2
    Peace
    Join Date
    Aug 2001
    Posts
    1,510
    Well, i didnt read your earlier post but i assume someone pointed out the strupr() function. So just run through your string on element at a time and check to see if the current element is a space ( strcmp(string[i], " "); ). If it is, then make the next element of the string uppercase. Stop when you get the to the Null Terminating character.
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

  3. #3
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    .
    Last edited by Troll_King; 11-30-2001 at 10:31 AM.

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    30
    char *ptr;

    ptr = your_sentence;

    for(int i=0; i<strlen(your_sentence); i++)
    if(ptr = ' ' && i+1 != strlen(your_sentence) && ptr > 'a' && ptr < 'z') {
    *ptr += ('A'-'a');
    ptr++;
    }

    Oddly enough, I think this will work...I have to brush up on pointers though.

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    30
    > if(ptr = ' ' && i+1 != strlen(your_sentence) && ptr > 'a' && ptr < 'z') {

    sorry, just realized that the second clause there should be:

    ... && i+1 < strlen(your_sentence) && ...

    > *ptr += ('A'-'a');

    also, this should be:

    *(ptr+1) += ('A' - 'a');

    In addition, a double space or tab, or any other non letter following a space will cause an infinite loop. So move the ptr++ outside the if statement, enclose in brackets inside the for statement. Well, there are probably more errors, this was supposed be a quick helpful bit of advice. Anyways.... just a suggestion.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 02:49 PM
  3. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 12:18 AM
  4. AnsiString versus char *
    By Zahl in forum C++ Programming
    Replies: 35
    Last Post: 10-16-2002, 08:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21