HamroCSIT Logo
HAMRO CSIT
  • Course New
  • Entrance
    • Entrance Preparation
    • MCQ Questions
    • Colleges
    • Entrance Class
    • Entrance Books
    • Free Entrance Video Course
  • Semester
    • First Semester
    • Second Semester
    • Third Semester
    • Fourth Semester
    • Fifth Semester
    • Sixth Semester
    • Seventh Semester
    • Eight Semester
  • Questions
  • Subscription Automated
  • Notices
  • Articles
  • More
    • Ask Question
    • College Ambassadors
    • Financial Support Program
    • Contribute
    • Contact Us
Login Register
Hamro CSIT User Account
  • Sign In
  • Create Account


Shape | Hamro CSIT Shape | Hamro CSIT Shape | Hamro CSIT Shape | Hamro CSIT

Article

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

Contents

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

  • Suresh Chand
  • 0 Comments
  • 6749 Views
  • 4 years ago

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 ([email protected])
 * @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
Login
Notify of
Please login to comment
0 Comments
Oldest
Newest Most Voted
Share

Share this link via

Or copy link

logoHAMROCSIT

Hamro CSIT is a comprehensive web and mobile platform that provides B.Sc. CSIT students with resources like notes, syllabi, question banks, solved past papers, practical files, and free entrance preparation materials — all in one place.

  • [email protected]
Semester
  • First Semester
  • Second Semester
  • Third Semester
  • Fourth Semester
  • Fifth Semester
  • Sixth Semester
  • Seventh Semester
  • Eighth Semester
Links
  • About Us
  • FAQs
  • Sitemap
  • Privacy Policy
  • Terms and Conditions
  • College Ambassadors
  • Financial Support Program
Hits Counter
20247164
Google Play App Store
Follow Us

Copyright 2026 | HAMROCSIT | All Right Reserved

Official Payment Partner Esewa Logo
HAMROCSIT.COM

Copyright 2024 | HAMROCSIT.COM | All Right Reserved - Nymna Technology