I'm pretty new to c, so please excuse the basic question. I'm trying to make a program for string permutations, I couldint do it, so I looked at the solution at http://www.cprogramming.com/challenges/permutesol.html . Now even after using this code, and trying to change it to c (from c++), it still doesnt work :( Here is my code, what could be the problem?
Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
void switch(char topermute[], int x, int y);
void permute(char topermute[], int place);
int main(int argc, char* argv[])
{
if(argc!=2)
{
printf("Usage: ./permute string");
}
else
{
permute(argv[1], 0);
}
return 0;
}
void switch(char topermute[], int x, int y)
{
char newstring[] = topermute[];
newstring[x] = newstring[y];
newstring[y] = topermute[x];
return newstring;
}
void permute(char topermute[], int place)
{
if(place == strlen(topermute) -1)
{
printf("%s", topermute);
}
for(int nextchar = place; nextchar < strlen(topermute); nextchar++)
{
permute(switch(topermute, place, nextchar),place+1);
}
}