Sentence Capitalizer

From the textbook:

Write a function that accepts a pointer to a C-String as an argument and capitalizes the first character of each sentence in the string. For instance, if the string argument is "hello. my name is Joe. what is your name?" the function should manipulate the string so it contains "Hello. My name is Joe. What is your name?" Demonstrate the function in a program that asks the user to input a string and then passes it to the function. The modified string should be displayed on screen. Optional Exercise: Write an overloaded version of this function that accepts a string class object as its argument.

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include<iostream>
#include<cctype>
#include<cstdlib>

using namespace std;

void capitalize(char sentence[], int const SIZE);

int main()
{
    const int SIZE = 1024;
    char sentence[SIZE];
    
    cout << "Enter a string:  " << endl << endl;
    cin.getline(sentence, SIZE);
    
    capitalize(sentence, SIZE);

    system("pause");
    return(0);
}

void capitalize(char sentence[], int SIZE)
{
     
     char *strPtr;
     int count = 0;
     
     sentence[0] = toupper(sentence[0]);
     
     for (int i = 0; i < SIZE; i++)
     {
         strPtr = strstr(sentence[i], ".");
         
         if (*strPtr == '.')
         {
                     *strPtr = toupper(*strPtr);
         }
     }
    
     while (sentence[count] != '\0')
     {
          cout << sentence[count];
          count++;
     }
}


Not even sure if I'm headed in the correct direction, but I'm getting the following errors:

1
2
3
4
E:\CPT-233\Sentence Capitalizer.cpp In function `void capitalize(char*, int)':
34 E:\CPT-233\Sentence Capitalizer.cpp call of overloaded `strstr(char&, const char[2])' is ambiguous  
note E:\CPT-233\<internal>:0 candidates are: char* std::strstr(const char*, const char*) <near match> 
note E:\CPT-233\<internal>:0                 char* std::strstr(char*, const char*) <near match> 


Anyone mind pointing out what I'm doing wrong?
strPtr = strstr(sentence[i], ".");
setence[i] is just one character (like 'r'), ¿how do you expect to search in a character?
Can't suggest a correction as I had no idea what were you trying to do.
Maybe?

sentence[i] == '.'

Like on line 35 and 41?


Edit: Actually, what are you trying to do?

sentence[i] == '.' will compare array element i against '.'

strstr(sentence, ".") will find the first string "." in the sentence

strchr(sentence, ".") will also find the first char '.' in the sentence

Or do you mean this?

strPtr = strstr(&sentence[i], ".") search from the i'th element?

If so, you probably want to alter your loop so it works with strstr's return value, rather than just stepping the index by one. (It might be easier to use a while loop and a pointer, rather than the index, in this particular case?)

Andy

PS Why are you trying to capitalize a full stop/period?
Last edited on
Topic archived. No new replies allowed.