Answer to Question #200150 in C++ for Imran hashmi

Question #200150

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


1
Expert's answer
2021-05-29T03:34:39-0400


#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog