Q1:-The user enters a sentence your task is to output every word on a new line.
*please note that i haven't learnt strings yet so please help me with it.*
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include<iostream>
#include<string>
usingnamespace std;
int main(){
cout<<"enter your sentence"<<endl;
string a;
getline(cin,a);
int b=a.length();
for(int i=0;i<b;i++){
if(a==" ")
cout<<endl;
else
cout<<a.substr(i,b);
}
return 0;}
Of course, you can fool that piece of code by not ending a sentence with a newline, for instance by entering a space then a newline at the termination of a sentence, thereby causing the loop to continue past the point where it should stop.
Making a helper function or two to advance to the next word in the sentence might help: