capitalizing using toupper

closed account (1bfSwA7f)
tttt
Last edited on
I was going to post this earlier but didn't get around to it. You can work out the intimate detail of working out what character starts a sentence :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>

void sentenceCapitalizer(char *ptr);

int main(){
    char userString[] =
    " nO, not 123 tonight. it's a very popular place \
    and you have to make reservations in advance. besides, \
    it's expensive, and I don't have any money.";
    
    std::cout << "The text BEFORE the modification:\n" << userString << '\n';
    
    sentenceCapitalizer(userString);
    
    std::cout << "\nThe text AFTER the modification:\n" << userString << '\n';
    
    return 0;
    
}

void sentenceCapitalizer(char *ptr)
{
    while(*ptr != '\0')
    {
        *ptr = toupper(*ptr);
        ptr++;
    }
}
What about this logic:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
1) declare a bool variable (let’s say ‘beginning’).
--> when ‘beginning’ is true, the next character you find must be capitalized;

2) initialize ‘beginning’ to true

3) loop through your C-style char array
   (note: consider switch)

    if you find a character like [period, exclamation mark…]
        set ‘beginning’ to true
        continue

    if you find a [space, comma… --> any character that can’t be capitalized]
        continue

    if you find a char that can be capitalized
        if ‘beginning’ == true
            capitalize that char
            set ‘beginning’ to false
            continue


(Note: I haven’t written the code - it’s just an hint)
closed account (1bfSwA7f)
@againtry thank you! that worked to capitalize the whole sentence ahah that was a lot of help

I'm trying to see if i can use an if statement for the punctuation and the spaces as well, but it isn't really clicking lol, im a newbie.
@osheikh8

Well that's good.

I suggest you follow through with your if statement idea and take careful note of the very strong hint from @Enoizat.

One small extra tip: AS a start, just go through the original string (array of characters with '\0' at its end) character by character in a new loop and change every 'a' to a '*' just to prove you can control what's going on. Then build up to do the real job with more complex if's with and's (&&) and or's (||) :)

(BTW you don't have to delete your MAX_LENGTH stuff necessarily, both work, my way is not restricted by any particular string length)
The sheik has ridden off on his camel by the look of it :(
Topic archived. No new replies allowed.