Print all possible strings (Function Problem)
PROBLEM :
Given a string str your task is to complete the function printSpaceString which takes only one argument the string str and prints all possible strings that can be made by placing spaces (zero or one) in between them.
For eg . for the string abc all valid strings will be
abc
ab c
a bc
a b c
Input:
The First line of input takes an integer T denoting the number of test cases . Then T test cases follow . Each line of test case contains a string str .
Output:
For each test case output the strings possible in a single line separated by a "$"
Constraints:
1<=T<=100
1<=length of string str <=100
Example:
Input
1
abc
Output
abc$ab c$a bc$a b c$
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/*
Please note that it's Function problem i.e.
you need to write your solution in the form of Function(s) only.
Driver Code to call/invoke your function would be added by GfG's Online Judge.*/
/*You have to complete this function*/
void Print(string temp,char str[],int index)
{
if(strlen(str)==index)
cout<<temp<<"$" ;
else
{
if(index!=0)
{
Print(temp+str[index],str,index+1) ;
Print(temp+" "+str[index],str,index+1) ;
}
else
Print(temp+str[index],str,index+1) ;
}
}
void printSpace(char str[])
{
Print("",str,0) ;
}
---------------------------------------------------------------------------------
Given a string str your task is to complete the function printSpaceString which takes only one argument the string str and prints all possible strings that can be made by placing spaces (zero or one) in between them.
For eg . for the string abc all valid strings will be
abc
ab c
a bc
a b c
Input:
The First line of input takes an integer T denoting the number of test cases . Then T test cases follow . Each line of test case contains a string str .
Output:
For each test case output the strings possible in a single line separated by a "$"
Constraints:
1<=T<=100
1<=length of string str <=100
Example:
Input
1
abc
Output
abc$ab c$a bc$a b c$
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/*
Please note that it's Function problem i.e.
you need to write your solution in the form of Function(s) only.
Driver Code to call/invoke your function would be added by GfG's Online Judge.*/
/*You have to complete this function*/
void Print(string temp,char str[],int index)
{
if(strlen(str)==index)
cout<<temp<<"$" ;
else
{
if(index!=0)
{
Print(temp+str[index],str,index+1) ;
Print(temp+" "+str[index],str,index+1) ;
}
else
Print(temp+str[index],str,index+1) ;
}
}
void printSpace(char str[])
{
Print("",str,0) ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment