Web Toolbar by Wibiya

Pages

Sunday, October 23, 2011

Given a string, print all possible uppercase and lowercase permutations of it.

Solution #1: This can be easily solved by recursion. Fix one letter to either uppercase or lowercase and permute rest of the letters. Here is the code,

#include 
#include 
#include 
#include 

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, "suchit maindola");

    toggle(str, 0);

    return 1;
}

No comments: