Given a string, print all possible uppercase and lowercase permutations of it.
Strategy:
Fix one letter to either uppercase or lowercase and permute rest of the letters
Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>//For toupper and tolower functions
void toggle(char* str, int n)
{
if(n == strlen(str))
{
printf("%s\n", str);
return;
}
str[n] = toupper(str[n]);
toggle(str, n+1);
str[n] = tolower(str[n]);
toggle(str, n+1);
}
int main()
{
char str[50] = {0, };
sprintf(str, "silu");
toggle(str, 0);
return 1;
}
Strategy:
Fix one letter to either uppercase or lowercase and permute rest of the letters
Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>//For toupper and tolower functions
void toggle(char* str, int n)
{
if(n == strlen(str))
{
printf("%s\n", str);
return;
}
str[n] = toupper(str[n]);
toggle(str, n+1);
str[n] = tolower(str[n]);
toggle(str, n+1);
}
int main()
{
char str[50] = {0, };
sprintf(str, "silu");
toggle(str, 0);
return 1;
}
Permutations definition
ReplyDeleteWhat is a permutation? , define Permutation, why we use permutation?
http://www.infoaw.com/article.php?articleId=945