Given a string we need to print the string with all possible combinations of the uppercase and lowercase characters in the string.
So given a string "abc", we need to print the following:
ABC
ABc
AbC
Abc
aBC
aBc
abC
abc
Solution is a simple recursive approach where we call the function again and again once with a character in lower case and another time with the same character in upper case. The code is similar to what we would write for all permutations of a string.
void lowerUpper(char *s, int start, int end) { if(start == end) { cout<<s<<endl; return; } //Change next character to upper case s[start] = toupper(s[start]); lowerUpper(s,start+1,end); //Change the same character as changed earlier to lower case s[start] = tolower(s[start]); lowerUpper(s,start+1,end); }
you can do the same by treating it in a form of truth table
ReplyDeleteuppercase=1 and lowercase=0
//to print all the combinations of the string for uppercase and lowercase
ReplyDelete#include
#include
#include
#include
void comb(char arr[],int len)
{
int i,nlen,index[50][50],j,k;
int n;
nlen=pow(2,len);
printf("All the possible combinations are:\n");
printf("%d\n",nlen);
for(i=0;i=0;k++,j--)
{
n=(int)(i/pow(2,j));
if(n%2==0)
putchar(tolower(arr[k]));
else
putchar(toupper(arr[k]));
}
printf("\n");
}
}
int main()
{
int t,len;
char arr[10];
printf("Enter the string\n");
gets(arr);
len=strlen(arr);
printf("The length of the string is %d\n",len);
comb(arr,len);
scanf("%d",&t);
return 0;
}
Sure you can.
DeleteI am not sure though if I understood your code. Whats the index array for?
Also j is never initialized and k keeps on increasing and whats the check for the for loop?