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 s;
cout<<"Input string to print substrings: ";
cin>>s;
int count = 0;
for(int i = s.length() - 1; i >= 0; i--){
for(int j = 0; j <= i; j++){
cout<<s[j];
count = j;
for(int k = i; k < s.length() - 1; k++){
cout<<s[count + 1];
count++;
}
count = 0;
cout<<endl;
}
}
return 0;
}
Comments
Leave a comment