string functions (beginner)
I've been agonizing over this all day. The assignment is to capitalize every other word in a user input string.
My logic is as follows:
1. I have the program get each character until it encounters white space (using for loop)
2. Then it should capitalize each character after the white space until it encounters another white space (using while loop).
My problem though is when i try to create a condition for while loop i have to terminate is when white space is encountered, but the very first character to start the while loop is a white space.. :(
I was told to define boolean values, but after trying a few boolean conditions I'm still stuck..
Thank you!!
Code:
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main ()
{
char nextChar;
int i;
bool space;
string string1;
cout<<"Enter a string:\n";
getline(cin,string1);
for(i=0;i<string1.length();i++)
{
nextChar=string1.at(i);//get a character
if(isspace(nextChar))
{
space=true;
while() ---> STUCK HERE
{
string1[i]=toupper(string1.at(i));
}
}
else
{
}
}
cout<<string1<<endl;
return 0;
}