Problem statement:- Program to print all permutations of a string using recursion.
Data requirement:-
Input Data:- str
Output Data:-str
Output Data:-str
Program in C
Here is the source code of the C Program to print all permutations of a string using recursion.
Code:
#include<stdio.h>
#include<string.h>
void rotate(char str[],int n);
void doAnagram(char str[], int n);
void display(char str[]);
void rotate(char str[],int n)
{
int j,size=strlen(str);
int p=size-n;
char temp=str[p];
for(j=p+1;j<size;j++)
str[j-1]=str[j];
str[j-1]=temp;
}
void doAnagram(char str[], int n)
{
if(n==1)
return;
for(int j=0;j<n;j++)
{
doAnagram(str,n-1);
if(n==2)
{
display(str);
}
rotate(str,n);
}
}
void display(char str[])
{ int size=strlen(str),j;
for(j=0; j<size; j++)
printf("%c",str[j]);
printf(" ");
}
int main()
{
char str[30];
printf("Enter your String:");
scanf("%[^\n]",str);
printf("All permutations of a Given string are:");
doAnagram(str,strlen(str));
}
Enter your String:aba
All permutations of a Given string are:aba aab baa baa aab aba
Program in C++
Here is the source code of the C++ Program to print all permutations of a string using recursion.
Code:
#include<iostream>
#include<string.h>
using namespace std;
void rotate_s(char str[],int n);
void doAnagram(char str[], int n);
void display(char str[]);
void rotate_s(char str[],int n)
{
int j,len=strlen(str);
int p=len-n;
char temp=str[p];
for(j=p+1;j<len;j++)
str[j-1]=str[j];
str[j-1]=temp;
}
void doAnagram(char str[], int n)
{
if(n==1)
return;
for(int j=0;j<n;j++)
{
doAnagram(str,n-1);
if(n==2)
{
display(str);
}
rotate_s(str,n);
}
}
void display(char str[])
{ int len=strlen(str),j;
for(j=0; j<len; j++)
printf("%c",str[j]);
printf(" ");
}
int main()
{
char str[30];
cout<<"Enter your String:";
cin>>str;
cout<<"All permutations of a Given string are:";
doAnagram(str,strlen(str));
}
Enter your String:bcaa
All permutations of a Given string are:bcaa bcaa baac baca baca baac caab caba caba caab cbaa cbaa aabc aacb abca abac acab acba abca abac acab acba aabc aacb
Program in Java
Here is the source code of the Java Program to print all permutations of a string using recursion.
Code:
import java.util.Scanner;
public class AnagramString {
static void rotate(char str[],int n)
{
int j,size=str.length;
int p=size-n;
char temp=str[p];
for(j=p+1;j<size;j++)
str[j-1]=str[j];
str[j-1]=temp;
}
static void doAnagram(char str[], int n)
{
if(n==1)
return;
for(int j=0;j<n;j++)
{
doAnagram(str,n-1);
if(n==2)
{
display(str);
}
rotate(str,n);
}
}
static void display(char str[])
{ int size=str.length,j;
for(j=0; j<size; j++)
System.out.print(str[j]);
System.out.print(" ");
}
public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
String str1;
System.out.print("Enter your String:");
str1=cs.nextLine();
char str[]=str1.toCharArray();
System.out.print("All permutations of a Given string are:");
doAnagram(str,str.length);
cs.close();
}
}
Enter your String:cat
All permutations of a Given string are:cat cta atc act tca tac
Most Recommend Questions:-
More Questions:-
0 Comments
Please do not Enter any spam link in the comment box