For example, entering C1 = A and C2 = C and N = 3 can print ABC ACB BAC You are
ID: 3798323 • Letter: F
Question
For example, entering C1 = A and C2 = C and N = 3 can print
ABC
ACB
BAC
You are to write a C language function called Combo that takes in three parameters: The first two parameters are character variables C1 and C2, and the third is an integer, N The characters C1 and C2 are guaranteed to be capitals only, and that C1 comes before C2 in the alphabet. Your function must print exactly N three letter 'words' that only use the range of letters from C1 to C2 in all three positions. In addition, none of the letters that are printed in any given word can be the same. If N is larger than the number of possible such words, then the program should simply print all possible words. One word should be printed per line. Note that there may be different ways to select the letters in the words that meet the specifications given above; any allowable three letters are ok, as long as they meet the specificationsExplanation / Answer
allwords.c :
#include <stdio.h>
#include <string.h>
#include<conio.h>
void Combo(char,char,int);
void find_rotations(char*,int,int);
void change_chars(char*,char*);
int total;
int main()
{
char C1,C2;
int N;
clrscr();
printf("Enter two characters:");
scanf("%c %c",&C1,&C2);
printf("Enter N value:");
scanf("%d",&N);
Combo(C1,C2,N);
getch();
return 0;
}
void Combo(char c1,char c2,int n){
char m;
int i,len=0;
char str[30];
for(m=c1,i=0;m<=c2;m++,i++)
str[i] = m;
len = strlen(str);
total = n;
find_rotations(str,0,len-1);
}
void find_rotations(char *p, int st, int en)
{
int i;
if (st == en &&total>0){
printf("%s ",p);
total--;
}
else
{
for (i = st; i <= en; i++)
{
change_chars((p+st),(p+i));
find_rotations(p,st+1,en);
change_chars((p+st), (p+i));
}
}
}
void change_chars(char *a, char *b)
{
char t;
t = *a;
*a = *b;
*b = t;
}
Sample Input & Output :
Enter two characters: A C
Enter N value:3
ABC
ACB
BAC
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.