Contents

Write a Program to get string and print all possible prefix, suffix and substring combinations.

In this program, We will take input from user as string and print all the possible prefix, suffix and substring combinations.

Let’s look on program:

Program:

/**
 * @author Suresh Chand (sureshchand12a@gmail.com)
 * @link https://hamrocsit.com
 * @brief C++ Program to get string from user and print all possible prefix, suffix and substring combinations
 * @version 0.1
 * @date 2022-04-07
 * 
 * @copyright Copyright (c) 2022
 * 
 */

#include<iostream>
using namespace std;

//Func Prototype
void prefix(string);
void suffix(string);
void substring(string);

int main(){
    string str;
    int choice;

    cout << "Enter the string: ";
    cin >> str;

    do{
        cout << "\n1. Prefix Combination\n2. Suffix Combination\n3. Substring Combination\n4. Other Number to exit\n\nEnter your choice: ";
        cin >> choice;

        switch(choice){
            case 1:
                prefix(str);
                break;

            case 2:
                suffix(str);
                break;

            case 3:
                substring(str);
                break;

            default:
                choice = 0;
                cout << "Invalid Option. Program Exit!!!" << endl;
                break;
        }
    }while(choice != 0);
}

void prefix(string s){
    cout << "\nPrefix Combination are:- " << endl;

    string temp;
    for(int i = 0; i < s.length(); i++){
        temp += s[i];
        cout << temp << endl;
    }

    cout << endl << endl;
}

void suffix(string s){

    cout << "\nSuffix Combination are:- " << endl;
    string temp;

    for(int i = s.length() - 1; i >= 0; i--){
        temp = s[i] + temp;
        cout << temp << endl;
    }

    cout << endl << endl;
}

void substring(string s){

    cout << "\nSubstring Combination are:- " << endl;

    for (int i = 0; i < s.length(); i++) {
        string temp;
        for (int j = i; j < s.length(); j++) {
            temp += s[j];
            cout << temp << endl;
        }
    }

    cout << endl << endl;
}

You can replace the substring() function using below function also. We have used default substr function on below code. Both code worked same as above:

void substring(string s){

    cout << "\nSuffix Combination are:- " << endl;
    int n = s.length();

    for (int i = 0; i < n; i++)
        for (int len = 1; len <= n - i; len++)
            cout << s.substr(i, len) << endl;

    cout << endl << endl;
}

Output:

The output of above program is:

Enter the string: abc

1. Prefix Combination
2. Suffix Combination
3. Substring Combination
4. Other Number to exit

Enter your choice: 1

Prefix Combination are:- 
a
ab
abc



1. Prefix Combination
2. Suffix Combination
3. Substring Combination
4. Other Number to exit

Enter your choice: 2

Suffix Combination are:-
c
bc
abc



1. Prefix Combination
2. Suffix Combination
3. Substring Combination
4. Other Number to exit

Enter your choice: 3

Substring Combination are:-
a
ab
abc
b
bc
c



1. Prefix Combination
2. Suffix Combination
3. Substring Combination
4. Other Number to exit

Enter your choice: 4
Invalid Option. Program Exit!!!
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments