Program to print all substrings of a given string in C++

C++Server Side ProgrammingProgramming

In this tutorial, we will be discussing a program to print all the substring of a given string.

For this we will be given with a string or an array of characters. Our task is to print all the substrings of that particular string.

Example

 Live Demo

#include<bits/stdc++.h>
using namespace std;
//printing all the substrings
void print_substr(char str[], int n){
   for (int len = 1; len <= n; len++){
      for (int i = 0; i <= n - len; i++){
         int j = i + len - 1;
         for (int k = i; k <= j; k++)
         cout << str[k];
         cout << endl;
      }
   }
}
int main(){
   char str[] = "abca";
   print_substr(str, strlen(str));
   return 0;
}

Output

a
b
c
a
ab
bc
ca
abc
bca
abca
raja
Published on 19-Dec-2019 15:07:21
Advertisements