I'm writing a program to generate a string of random uppercase letters, then take user input of uppercase letters, along with a character form the user. For any instance of the user input letter in the random string, it replaces that letter with the character entered by the user.
For example, s1 = {BDHFKYL}
s2 = {YEIGH}
c = '*'
Output = BD*FK*L
The program is based in a loop and asks if you would like to enter in another string to replace. When I enter a 'y'
to enter another loop I get this:
Please enter at least 2 capital letters and a maximum of 20.
HAJSKSMDHSJ
HAJSKSMDHSJ
NWLRBB*QB*C**RZOW**Y*I**Q*C*XR**OWFRX**Y
Would you like to enter another string?
y -(HERE"S WHERE THE PROBLEM IS)-
Please enter at least 2 capital letters and a maximum of 20.
You need at least two letters
Would you like to enter another string?
Any suggestions? Thank you in advance.
void fillS1(char x[]);
void fillS2(char x[], char y[], char z);
void strFilter(char a[], char b[], char c);
int main(int argc, const char * argv[])
{
char s1[42];
char s2[22];
fillS2(s2, s1, '*');
return 0;
}
void fillS1(char x[])
{
for (int i = 0; i < 40; i++)
x[i] = 'A' + random() % 26;
x[40] = (char)0;
}
void fillS2(char x[], char y[], char z){
char loopContinue = 0;
do {
int i = 0;
printf("Please enter at least 2 capital letters and a maximum of 20.\n");
while (( x[i] = getchar()) != '\n' ) {
i++;
}
x[i] = '\0';
if (i < 3) {
printf("You need at least two letters\n");
}
else if (i > 21){
printf("You cannot have more than twenty letters\n");
}
else if (i > 0){
for (i = 0; i < 20; i++) {
if ((x[i] >= 'A') && (x[i] <= 'Z')) {
puts(x);
fillS1(y);
strFilter(y, x, '*');
break;
}
}
}
printf("Would you like to enter another string?\n");
scanf("%c", &loopContinue);
} while (loopContinue != 'n');
}
void strFilter(char a[], char b[], char c){
int i = 0;
int n = 0;
while (n < 20) {
for (i = 0; i < 40; i++) {
if (a[i] == b[n]){
a[i] = c;
}
}
i = 0;
n++;
}
puts(a);
}