我正在尝试随机更改字符串中的随机字符。程序还将决定随机变化的角色数量。编译器没有看到第二个for循环。我不知道为什么?再次感谢您
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
char string[10];
srand(time(NULL));
int a;
int count = 0;
printf("Please enter string: ");
scanf("%s", string);
for (a = 0; string[a] != '\0'; a++) {
count++;
}
printf("%d\n", count);
for (int i = 0; i <= count; i + rand() % count) {
string[i];
}
printf("String is: %s ", string);
}
答案 0 :(得分:1)
要在单词中选择随机char
并将其替换为随机char
,您可以尝试以下操作。
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
char string[10];
srand(time(NULL));
int a;
int count = 0;
printf("Please enter string: ");
scanf("%s", string);
for (a = 0; string[a] != '\0'; a++) {
count++;
}
printf("%d\n", count);
char randomletter = 'a' + (random() % 26);
string[rand() % count] = randomletter;
printf("String is: %s ", string);
}
测试
Please enter string: foobar
6
String is: fooiar
测试2
Please enter string: zoobar
6
String is: zolbar