okay so I have to write a program that accepts input from user, a sentence in which all of the words are together, but the first character of each word is uppercase. I have Convert the sentence to a string in which the words are separated by spaces and only the first word starts with an uppercase letter. Print the converted string to screen
ex: so the string “StopAndSmellTheRoses.” would be converted to “Stop and smell the roses” and printed.
this is what I've done so far and I'm not sure what I'm doing wrong.
int main (){
const int SIZE=80;
char strArr[SIZE];
cout<< "please enter a sentence with no space and make the first letter of each word capital"<<endl;
cin.getline(strArr,SIZE);
int i=0;
while(strArr[i]!='\0'){
if(isupper(strArr[i])){
strArr.insert(i,"");
}
}
Please use the code-tags (the "<>" button) when posting code.
Here is how I would approach this task.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
int main ()
{
std::string inputText = "StopAndSmellTheRoses."; // if you use a string instead of a char array you are not restricted by a predefined length.
for (int i=0; i<inputText.size(); i++) // you can ask a string how long it is, making it easier to iterate
{
std::cout << inputText[i]; // you don't have to change anything in the string or in your array, that will only make things
// more difficult. (especially with the char array as you will have to move everything that
// comes after a uppercase letter before yo can insert the space).
if (isupper(inputText[i+1]))
{
std::cout << " "; // if the next character is a capital, insert the space
inputText[i+1] = tolower(inputText[i+1]);// and change it to a lower case before we print the character
}
}
return 0;
}