我不知道我的代码有什么问题,但是当我编译时,我得到:
warning: passing arg 2 of `strcspn' makes pointer from integer without a cast
这是代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STR_LEN 50
int main(void) {
int i = 0, j = 0, length = 0, count1 = 0, count2 = 0, count3 = 0;
char letter3 = 'a', letter2 = 'a', string[STR_LEN] = { 0 };
length = strlen(string);
printf("Enter a sentence: ");
fgets(string, STR_LEN, stdin);
for (i = 0; i < length; i++) {
for (j = 0; j < length; j++) {
if (string[i] == string[j]) {
count1++;
} else {
count1 = 0;
}
}
if (count1 > count3) {
count2 = count3;
count3 = count1;
letter2 = letter3;
letter3 = string[i];
} else
if (count1 > count2) {
count2 = count1;
letter2 = string[i];
}
}
string[strcspn(string, letter2)] = letter3;
string[strcspn(string, letter3)] = letter2;
printf("\n %s", string);
system("pause");
return 0;
}
该代码应该从用户那里得到一个句子,并将句子中最常见的字母与第二个常见字母切换。