Print all possible strings
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 :
--------------------------------------------------------------------------------
/*You have to complete this function*/
void Print(char [],char [],int ,int ,int ) ;
void printSpace(char str[])
{
int len=strlen(str) ;
char arr[2*len] ;
arr[0]=str[0] ;
Print(str,arr,1,1,len) ;
}
void Print(char str[],char arr[],int curr,int i,int len)
{
if(curr==len)
{
arr[i]='\0' ;
cout<<arr<<"$" ;
return ;
}
arr[i]=str[curr] ;
Print(str,arr,curr+1,i+1,len) ;
arr[i]=' ' ;
arr[i+1]=str[curr] ;
Print(str,arr,curr+1,i+2,len) ;
}
---------------------------------------------------------------------------------
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 :
--------------------------------------------------------------------------------
/*You have to complete this function*/
void Print(char [],char [],int ,int ,int ) ;
void printSpace(char str[])
{
int len=strlen(str) ;
char arr[2*len] ;
arr[0]=str[0] ;
Print(str,arr,1,1,len) ;
}
void Print(char str[],char arr[],int curr,int i,int len)
{
if(curr==len)
{
arr[i]='\0' ;
cout<<arr<<"$" ;
return ;
}
arr[i]=str[curr] ;
Print(str,arr,curr+1,i+1,len) ;
arr[i]=' ' ;
arr[i+1]=str[curr] ;
Print(str,arr,curr+1,i+2,len) ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment