Q: Write a function to print all of the permutations of a string.
A: I wrote a recursive function to fulfill this. I was wondering if there's a better way to do this? either simpler or having less Runtime...
Thanks!
#include <string.h>
#include <iostream.h>
/*Remove one character from str*/
void strrmv(const char *str, char* newstr,int removethis)
{
for (int i=0; i<removethis; i++){
*(newstr+i)=*(str+i);
}
for(int i=removethis+1; i<strlen(str); i++){
*(newstr+i-1)=*(str+i);
}
*(newstr+i-1)='\0';
}
void permuString(char *a, int size)
{
if (size==1){
cout<<a<<endl;
return;
}
else
{
for (int j=0; j<size; j++)
{
cout << a[j];
char *temp= new char[size];
strrmv(a,temp,j)
permuString(temp, size-1);
delete temp;
}
}
}
void main()
{
char instr[]="abcd";
int l = strlen(instr);
permuString(instr,l);
}
//p.s.: what's the best way to create an array dynamicly according to the input? Should I use new, delete? or should I use vector?
A: I wrote a recursive function to fulfill this. I was wondering if there's a better way to do this? either simpler or having less Runtime...
Thanks!
#include <string.h>
#include <iostream.h>
/*Remove one character from str*/
void strrmv(const char *str, char* newstr,int removethis)
{
for (int i=0; i<removethis; i++){
*(newstr+i)=*(str+i);
}
for(int i=removethis+1; i<strlen(str); i++){
*(newstr+i-1)=*(str+i);
}
*(newstr+i-1)='\0';
}
void permuString(char *a, int size)
{
if (size==1){
cout<<a<<endl;
return;
}
else
{
for (int j=0; j<size; j++)
{
cout << a[j];
char *temp= new char[size];
strrmv(a,temp,j)
permuString(temp, size-1);
delete temp;
}
}
}
void main()
{
char instr[]="abcd";
int l = strlen(instr);
permuString(instr,l);
}
//p.s.: what's the best way to create an array dynamicly according to the input? Should I use new, delete? or should I use vector?