Write a C++ program to accept a sentence
in one line from user and displays its individual words
on seperate line along with their length as below...
This --> 4
is --> 2
a --> 1
Test --> 4
String -->6
#include <iostream>
using namespace std;
void spWord(string s)
{
string wd = "";
for (auto a : s)
{
if (a == ' ')
{
int n=wd.length();
cout << wd << " -> "<<n<<endl;
wd = "";
}
else {
wd = wd + a;
}
}
int n=wd.length();
cout << wd << " -> "<<n<<endl;
}
int main()
{
string sentence = "This Is a Test String";
spWord(sentence);
return 0;
}
Comments
Leave a comment