Write a program that reads a word and prints all sub strings sorted by length. For example, if the user enters the word ”cat”, the program prints
c
a
t
ca
at
cat
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
getline(cin, str);
int n = str.length();
for (int i=1; i<=n; i++) {
for (int j=0; j<=n-i; j++) {
cout << str.substr(j, i) << endl;
}
}
}
Comments
Leave a comment