Print all substrings of a given string

Here given code implementation process.

//C Program
//Print all substrings of a given string
#include <stdio.h>

void show(char str[],int start,int last,int size)
{

  if(last > size) 
  { 
    //Base case ,which is controlling recursion process
    return;
  }

  for (int i = start; i <=last ; ++i)
  {
    printf("%c",str[i] );
  }
 
  printf("\n");
  //Recursive execute to get next substring
  show(str,start,last+1,size);
}

void substrings(char str[],int size)
{
  printf("\nString : %s\n",str );

  for (int i = 0; i < size; ++i)
  {
    show(str,i,i,size-1); 
  }
}

int main()
{
  
  char str1[]="SILENCE";

  //Get the size of string text
  int size=sizeof(str1)/sizeof(str1[0])-1;

  substrings(str1,size);


  char str2[]="ABCDE";

  //Get the size of string text
  size=sizeof(str2)/sizeof(str2[0])-1;

  substrings(str2,size);

  return 0;
}

Output

String : SILENCE
S
SI
SIL
SILE
SILEN
SILENC
SILENCE
I
IL
ILE
ILEN
ILENC
ILENCE
L
LE
LEN
LENC
LENCE
E
EN
ENC
ENCE
N
NC
NCE
C
CE
E

String : ABCDE
A
AB
ABC
ABCD
ABCDE
B
BC
BCD
BCDE
C
CD
CDE
D
DE
E
// Java program
// Print all substrings of a given string

public class MyString {
  
  public void show(String str, int start, int last, int size) 
  {
    if (last > size) 
    {
      //Base case ,which is controlling recursion process
      return ;
    }
    for (int i = start; i <= last; ++i) 
    {
      System.out.print(""+str.charAt(i)+"");
    }
    System.out.print("\n");

    //Recursive execute to get next substring
    show(str, start, last + 1, size);
  }
  public void substrings(String str) 
  {
    int size = str.length();

    System.out.print("\nString : "+str+"\n");
    
    for (int i = 0; i < size; ++i) 
    {
      show(str, i, i, size - 1);
    }
  }
  public static void main(String[] args) 
  {

    MyString obj = new MyString();
    
    obj.substrings("SILENCE");
    obj.substrings("ABCDE");
  
  }
}

Output

String : SILENCE
S
SI
SIL
SILE
SILEN
SILENC
SILENCE
I
IL
ILE
ILEN
ILENC
ILENCE
L
LE
LEN
LENC
LENCE
E
EN
ENC
ENCE
N
NC
NCE
C
CE
E

String : ABCDE
A
AB
ABC
ABCD
ABCDE
B
BC
BCD
BCDE
C
CD
CDE
D
DE
E
// C++ program
// Print all substrings of a given string
#include<iostream>

using namespace std;
class MyString {
	public:
		void show(string str, int start, int last, int size) {
			if (last > size) {
				//Base case ,which is controlling recursion process

				return;
			}
			for (int i = start; i <= last; ++i) {
				cout << "" << str[i] << "";
			}
			cout << "\n";
			//Recursive execute to get next substring
			this->show(str, start, last + 1, size);
		}
	void substrings(string str) {
		int size = str.size();
		cout << "\nString : " << str << "\n";
		for (int i = 0; i < size; ++i) {
			this->show(str, i, i, size - 1);
		}
	}
};
int main() {
	MyString obj = MyString();
	obj.substrings("SILENCE");
	obj.substrings("ABCDE");
	return 0;
}

Output

String : SILENCE
S
SI
SIL
SILE
SILEN
SILENC
SILENCE
I
IL
ILE
ILEN
ILENC
ILENCE
L
LE
LEN
LENC
LENCE
E
EN
ENC
ENCE
N
NC
NCE
C
CE
E

String : ABCDE
A
AB
ABC
ABCD
ABCDE
B
BC
BCD
BCDE
C
CD
CDE
D
DE
E
// C# program
// Print all substrings of a given string
using System;
public class MyString {
	public void show(String str, int start, int last, int size) {
		if (last > size) {
			return;
		}
		for (int i = start; i <= last; ++i) {
			Console.Write("" + str[i] + "");
		}
		Console.Write("\n");
		show(str, start, last + 1, size);
	}
	public void substrings(String str) {
		int size = str.Length;
		Console.Write("\nString : " + str + "\n");
		for (int i = 0; i < size; ++i) {
			show(str, i, i, size - 1);
		}
	}
	public static void Main(String[] args) {
		MyString obj = new MyString();
		obj.substrings("SILENCE");
		obj.substrings("ABCDE");
	}
}

Output

String : SILENCE
S
SI
SIL
SILE
SILEN
SILENC
SILENCE
I
IL
ILE
ILEN
ILENC
ILENCE
L
LE
LEN
LENC
LENCE
E
EN
ENC
ENCE
N
NC
NCE
C
CE
E

String : ABCDE
A
AB
ABC
ABCD
ABCDE
B
BC
BCD
BCDE
C
CD
CDE
D
DE
E
<?php
// Php program
// Print all substrings of a given string
class MyString {
	public 	function show($str, $start, $last, $size) {
		if ($last > $size) {
			return;
		}
		for ($i = $start; $i <= $last; ++$i) {
			echo("". $str[$i] ."");
		}
		echo("\n");
		//Recursive execute to get next substring
		$this->show($str, $start, $last + 1, $size);
	}
	public 	function substrings($str) {
		$size = strlen($str);
		echo("\nString : ". $str ."\n");
		for ($i = 0; $i < $size; ++$i) {
			$this->show($str, $i, $i, $size - 1);
		}
	}
}

function main() {
	$obj = new MyString();
	$obj->substrings("SILENCE");
	$obj->substrings("ABCDE");

}
main();

Output

String : SILENCE
S
SI
SIL
SILE
SILEN
SILENC
SILENCE
I
IL
ILE
ILEN
ILENC
ILENCE
L
LE
LEN
LENC
LENCE
E
EN
ENC
ENCE
N
NC
NCE
C
CE
E

String : ABCDE
A
AB
ABC
ABCD
ABCDE
B
BC
BCD
BCDE
C
CD
CDE
D
DE
E
// Node Js program
// Print all substrings of a given string
class MyString {
	show(str, start, last, size) {
		if (last > size) {
			return;
		}

		for (var i = start; i <= last; ++i) {
			process.stdout.write("" + str[i] + "");
		}

		process.stdout.write("\n");
		//Recursive execute to get next substring
		this.show(str, start, last + 1, size);
	}
	substrings(str) {
		var size = str.length;
		process.stdout.write("\nString : " + str + "\n");
		for (var i = 0; i < size; ++i) {
			this.show(str, i, i, size - 1);
		}
	}
}

function main(args) {
	var obj = new MyString();
	obj.substrings("SILENCE");
	obj.substrings("ABCDE");
}

main();

Output

String : SILENCE
S
SI
SIL
SILE
SILEN
SILENC
SILENCE
I
IL
ILE
ILEN
ILENC
ILENCE
L
LE
LEN
LENC
LENCE
E
EN
ENC
ENCE
N
NC
NCE
C
CE
E

String : ABCDE
A
AB
ABC
ABCD
ABCDE
B
BC
BCD
BCDE
C
CD
CDE
D
DE
E
#  Python 3 program
#  Print all substrings of a given string
class MyString :
	def show(self, str, start, last, size) :
		if (last > size) :
			return
		
		i = start
		while (i <= last) :
			print("", str[i] ,"", end = "")
			i += 1
		
		print("\n", end = "")
		self.show(str, start, last + 1, size)
	
	def substrings(self, str) :
		size = len(str)
		print("\nString : ", str ,"\n", end = "")
		i = 0
		while (i < size) :
			self.show(str, i, i, size - 1)
			i += 1
		
	

def main() :
	obj = MyString()
	obj.substrings("SILENCE")
	obj.substrings("ABCDE")


if __name__ == "__main__":
	main()

Output

String :  SILENCE
 S
 S  I
 S  I  L
 S  I  L  E
 S  I  L  E  N
 S  I  L  E  N  C
 S  I  L  E  N  C  E
 I
 I  L
 I  L  E
 I  L  E  N
 I  L  E  N  C
 I  L  E  N  C  E
 L
 L  E
 L  E  N
 L  E  N  C
 L  E  N  C  E
 E
 E  N
 E  N  C
 E  N  C  E
 N
 N  C
 N  C  E
 C
 C  E
 E

String :  ABCDE
 A
 A  B
 A  B  C
 A  B  C  D
 A  B  C  D  E
 B
 B  C
 B  C  D
 B  C  D  E
 C
 C  D
 C  D  E
 D
 D  E
 E
#  Ruby program
#  Print all substrings of a given string
class MyString 
	def show(str, start, last, size) 
		if (last > size) 
			return
		end
		i = start
		while (i <= last) 
			print("", str[i] ,"")
			i += 1
		end
		print("\n")
		self.show(str, start, last + 1, size)
	end
	def substrings(str) 
		size = str.length()
		print("\nString  :", str ,"\n")
		i = 0
		while (i < size) 
			self.show(str, i, i, size - 1)
			i += 1
		end
	end
end
def main() 
	obj = MyString.new()
	obj.substrings("SILENCE")
	obj.substrings("ABCDE")
end
main()

Output

String  :SILENCE
S
SI
SIL
SILE
SILEN
SILENC
SILENCE
I
IL
ILE
ILEN
ILENC
ILENCE
L
LE
LEN
LENC
LENCE
E
EN
ENC
ENCE
N
NC
NCE
C
CE
E

String  :ABCDE
A
AB
ABC
ABCD
ABCDE
B
BC
BCD
BCDE
C
CD
CDE
D
DE
E
// Scala program
// Print all substrings of a given string
class MyString {
	def show(str: String, start: Int, last: Int, size: Int): Unit = {
		if (last > size) {
			return;
		}
		var i: Int = start;
		while (i <= last) {
			print("" + str(i) + "");
			i += 1;
		}
		print("\n");
		show(str, start, last + 1, size);
	}
	def substrings(str: String): Unit = {
		var size: Int = str.length();
		print("\nString : " + str + "\n");
		var i: Int = 0;
		while (i < size) {
			show(str, i, i, size - 1);
			i += 1;
		}
	}
}
object Main {
	def main(args: Array[String]): Unit = {
		var obj: MyString = new MyString();
		obj.substrings("SILENCE");
		obj.substrings("ABCDE");
	}
}

Output

String : SILENCE
S
SI
SIL
SILE
SILEN
SILENC
SILENCE
I
IL
ILE
ILEN
ILENC
ILENCE
L
LE
LEN
LENC
LENCE
E
EN
ENC
ENCE
N
NC
NCE
C
CE
E

String : ABCDE
A
AB
ABC
ABCD
ABCDE
B
BC
BCD
BCDE
C
CD
CDE
D
DE
E
// Swift program
// Print all substrings of a given string
class MyString {
	func show(_ data: String, _ start: Int, _ last: Int, _ size: Int) {
		if (last > size) {
			return;
		}
		var i: Int = start;
      	var str = Array(data);
		while (i <= last) {
			print("", str[i] ,"", terminator: "");
			i += 1;
		}
		print("\n", terminator: "");
		self.show(data, start, last + 1, size);
	}
	func substrings(_ str: String) {
		let size: Int = str.count;
		print("\nString : ", str ,"\n", terminator: "");
		var i: Int = 0;
		while (i < size) {
			self.show(str, i, i, size - 1);
			i += 1;
		}
	}
}
func main() {
	let obj: MyString = MyString();
	obj.substrings("SILENCE");
	obj.substrings("ABCDE");
}
main();

Output

String :  SILENCE
 S
 S  I
 S  I  L
 S  I  L  E
 S  I  L  E  N
 S  I  L  E  N  C
 S  I  L  E  N  C  E
 I
 I  L
 I  L  E
 I  L  E  N
 I  L  E  N  C
 I  L  E  N  C  E
 L
 L  E
 L  E  N
 L  E  N  C
 L  E  N  C  E
 E
 E  N
 E  N  C
 E  N  C  E
 N
 N  C
 N  C  E
 C
 C  E
 E

String :  ABCDE
 A
 A  B
 A  B  C
 A  B  C  D
 A  B  C  D  E
 B
 B  C
 B  C  D
 B  C  D  E
 C
 C  D
 C  D  E
 D
 D  E
 E


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved