Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

#include <iostream> #include <string> using namespace std; void print_all_suffix

ID: 3812425 • Letter: #

Question

#include <iostream>

#include <string>

using namespace std;

void print_all_suffix(string s)

{

  int l = s.length();

  for(int i = l; i >=0 ; i--)

   {

    string ss = s.substr(i,l);

    if(ss.empty())

      cout << "empty string" << endl;

    else

      cout << ss << endl;

      cout<< i+1 <<" ";

   }

}

void recursive_fun(string ss)

{

   // recursive_fun()

}

int main()

{

  string s;

  cout << "Enter a string: ";

  cin >> s;

  cout << s << endl;

  print_all_suffix(s);

  recursive_fun(s);

}

Test the given code. It generates all possible suffixes from a given string Task 1:(5 points) Modify the print all suffix(string) function to count the total number of suffixes and print the number besides printing the suffixes Task 2:(10 points) Write a recursive function to generate all possible suffixes. Task 3:(5 points): How can you count the total number of suffixes without generating them?

Explanation / Answer

My understanding of the question 1 is to print the number of the suffix beside it. I have modified print_all_suffix function to do this. For second question i have written recursive function. For the third you can make a simple calculation to find number of suffixes of a string. It is nothing but stringlength(string). Yes, the string length of the suffix is the number of suffixes.

#include <iostream>
#include <string>

using namespace std;

void print_all_suffix(string s)
{
int l = s.length();
for(int i = l; i >=0 ; i--)
{
string ss = s.substr(i,l);
if(ss.empty())
cout << "empty string" << endl;
else
cout << i+1 << " "<<ss << endl;
}
}

void recursive_fun(string ss)
{
   if(ss.empty())
       return;
   recursive_fun(&(ss[1]));
   cout<<ss<<endl;
}

int main()
{
string s;

cout << "Enter a string: ";
cin >> s;
cout << s << endl;

print_all_suffix(s);
cout<<"Printing all suffixes using recursive function"<<endl;
recursive_fun(s);


}

Output:

Enter a string: sleep
sleep
empty string
5 p
4 ep
3 eep
2 leep
1 sleep
Printing all suffixes using recursive function
p
ep
eep
leep
sleep