Sentence Capitalizer C++

I am writing this program and need it to capitalize the first letter of each sentence, but it is not working.

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
47
48
49
50
51
52
#include <iostream>
#include <iomanip>
#include <string>
#include <cstring>
#include <cctype>
using namespace std;

char *capitalize(char *sentence);

int main()
{
	const int size = 500;
	char usersentence[size];	//user inputed text/sentences

	cout << "Input a string to modify: ";
	cin.getline(usersentence, size);

	cout << "\nYour modified string is below.\n\n";

	cout << capitalize(usersentence);

	cout << endl << endl;
	system("pause");
}

//fnc to capitalize each sentence
char *capitalize(char *sentence)
{
	int length = strlen(sentence);		//used to get the length of user string

	int i, j;	//integers for the for-loop. Will use both to decide char to capitalize

	if (islower(sentence[0]))		//capitalize the first letter
	{
		toupper(sentence[0]);
	}

	for (i = 0; i < length; i++)	//used to search the string
	{
		j = i;

		if (sentence[i] == '.' || sentence[i] == '?' || sentence[i] == '!')		//to search for punctuation
		{
			j++;
			toupper(sentence[j]);
		}
	}

	return sentence;
}

Last edited on
What's wrong with it? Why is it not working? What errors does it produce?

What is the intended result? What result is it giving? Why might it be giving that result?
Aren't you capitalizing the whit space after each ./?/!
toupper doesn't actually change the values in the string, it just returns the capitalized version of that string/char. Where you have toupper(sentence[0]);
you should have
sentence[0] = toupper(sentence[0]);

Checking to see if the first letter is lower is redundant as toupper will still return the capitalized version of the string. So you can get rid of the if (islower(sentence[0]))

And Mahrgell is right, if there is a whitespace (eg. a space) after a ./?/! then it will try and capitalize the white space. Instead of capitalizing the next character in the string you should search forwards until you find a letter, _then_ toupper it
Ok I am going to try that. But I thought that where I put j++ it moves the the right of the period, etc, and caps that letter. I am going to make changes from Lachlan and see if I can get it to work.

EDIT:
Also there are no errors. It is just displaying the original string. for Noxzema

EDIT(2):

I got it working, thanks. lol I didn't realize I was going over one to the space and not going over again to the letter lol. here is final code.

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
47
48
49
50
51
52
53
54
#include <iostream>
#include <iomanip>
#include <string>
#include <cstring>
#include <cctype>
using namespace std;

char *capitalize(char *sentence);

int main()
{
	const int size = 500;
	char usersentence[size];	//user inputed text/sentences

	cout << "Input a string to modify: ";
	cin.getline(usersentence, size);

	cout << "\nYour modified string is below.\n\n";

	cout << capitalize(usersentence);

	cout << endl << endl;
	system("pause");
}

//fnc to capitalize each sentence
char *capitalize(char *sentence)
{
	int length = strlen(sentence);		//used to get the length of user string

	int i, j;	//integers for the for-loop. Will use both to decide char to capitalize

	sentence[0] = toupper(sentence[0]);	//capitalize the first letter

	for (i = 0; i < length; i++)		//used to search the string
	{
		j = i;

		if (sentence[i] == '.' || sentence[i] == '?' || sentence[i] == '!')		//to search for punctuation
		{
			j++;
			j++;
			sentence[j] = toupper(sentence[j]);
			
			if (sentence[j] == ' ')		//for doulble spaced sentences
			{
				j++;
				sentence[j] = toupper(sentence[j]);
			}
		}
	}

	return sentence;
}
Last edited on
Topic archived. No new replies allowed.