我正在编写一个程序来生成一串随机大写字母,然后获取用户输入的大写字母以及用户的字符。对于随机字符串中用户输入字母的任何实例,它会将该字母替换为用户输入的字符。
例如,s1 = {BDHFKYL}
s2 = {YEIGH}
c = '*'
Output = BD*FK*L
该程序基于一个循环,并询问您是否要输入另一个字符串来替换。当我输入 a'y'
进入另一个循环时,我得到了这个:
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?
有什么建议么?先感谢您。
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);
}