I have a string which is string s = "Hello:Brave:New:World";
We are supposed to used a while loop to make a new line where ever there is a ":" and then remove the colons. My professor wants me to use two unsigned ints which is confusing me somewhat. This is what I have so far:
/*
Classwork1
2.1.11
*/
#include<iostream>
#include<string>
usingnamespace std;
int main()
{
unsignedint found(0), temp(0);
string s = "Hello:Brave:New:World";
string ss;
//write a loop to print each word on own line without colon
while(found < s.size())
{
found = s.find(":", found-temp);
ss = s.substr(temp,(found-temp));
cout << ss << endl;
temp = found + 1;
}
cout << endl;
return 0;
}
Here is what I get when I run it:
Hello
Brave:New:World
Press any key to continue . . .
Desk test: Initial state
s="Hello:Brave:New:World";
found=0, temp=0
1 2 3 4 5 6
found = s.find(":", found-temp); //found-temp=0 -> found=5
ss = s.substr(temp,(found-temp)); //ss="Hello"
cout << ss << endl;
temp = found + 1;//temp=6
//next loop
found = s.find(":", found-temp); //found-temp=-1 but it is unsigned so max_int -> found=string::npos