#include <iostream>
#include <iomanip>
#include <string>
#include <cstring>
#include <cctype>
usingnamespace std;
char *capitalize(char *sentence);
int main()
{
constint 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;
}
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.
#include <iostream>
#include <iomanip>
#include <string>
#include <cstring>
#include <cctype>
usingnamespace std;
char *capitalize(char *sentence);
int main()
{
constint 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;
}