以下是我編寫的用於在字符串迴文中查找缺失字符的代碼,但我不知道如何從兩側檢查字符串時如何查找必須打印的字符同時。查找字符串迴文中缺少的字符
#include <stdio.h>
#include <stdlib.h>
int main() {
char s[100], m[100];
fgets(s, 100, stdin);
int len = strlen(s);
int a, b;
for (int i = 0; i < strlen(s); i++) {
m[i] = s[len - i - 1];
}
for (int i = 0, j = 0; i <= strlen(s), j <= strlen(s); i++, j++) {
if (isspace(m[j])) {
j++;
}
if (s[i] != m[j]) {
if (len % 2 == 0) {
printf("%c", m[j]);
break;
} else {
printf("%c", s[i]);
break;
}
}
}
}
輸入:Malayaam
輸出:l
輸入:abcddcb
輸出:a
'I <= strlen的(S),J <= strlen的(S)'不會做你認爲它應該。 – mch
具有挑戰性的測試案例「ZZxyxyxxyxyZZ」(刪除左邊的x)與「ZZxyxyyxyxyZZ」(刪除右邊的y)。 – chux
@chux不錯的一個。但我認爲命名一個字母添加被接受的解決方案的問題「找到失蹤的......」 – Yunnosch