C的回文计划

我在C中的程序是Palindrome,其function有误。 我的function是不比较我的字符串中的2个字符。 当我输入单个字符时,它会回答回文,但如果它是两个或更多,则总是不回文。

码:

int IntStrlength=strlen(StrWord); int IntCtr2=0; int IntCtr=1, IntAnswer; while(IntCtr<=(IntStrlength/2)){ printf(" %d %d\n", IntCtr2,IntStrlength); if(StrWord[IntStrlength] != StrWord[IntCtr2]){ IntAnswer=0; printf(" %d=Not Palindrome", IntAnswer); exit (0); }//if(StrWord[IntCtr2]!=StrWord[IntStrlength]) <--------- else{ IntCtr2++; IntStrlength--; }// else <-------- IntCtr++; }//while(IntCtr<IntStrlength/2) <----------- IntAnswer=1; printf(" %d=Palindrome", IntAnswer); return ; 

}

单个字符:

两个或多个字符:

为什么不这样写呢

 int wordLength = strlen(StrWord); for (int i=0;i<(wordLength/2);i++) { if (StrWord[i] != StrWord[wordLength-i-1]) { return 0; } } return 1; 

对于长度均匀(例如8)的单词,计数器将从0到3,访问所有字母。 对于不均匀的单词(比如7),counter将从0变为2,中间元素不受控制。 这不是必要的,因为它是一个回文并且总是匹配自己

我之前在一本名为“Cracking the Coding Interview”的采访书中看过这个算法。

在其中,作者展示了一个非常简单易用的代码实现。 代码如下: 这里还有一个解释代码的video。

 #include #include // strlen() void isPalindrome(char str[]); int main(){ isPalindrome("MOM"); isPalindrome("M"); return 0; } void isPalindrome(char str[]){ int lm = 0;//left most index int rm = strlen(str) - 1;//right most index while(rm > lm){ if(str[lm++] != str[rm--]){ printf("No, %s is NOT a palindrome \n", str); return; } } printf("Yes, %s is a palindrome because the word reversed is the same \n", str); } 
 #include int check_palindrom(char *); int main() { char s1[20]; printf("Enter the string...\n"); gets(s1); int x; x=check_palindrom(s1); x?printf("Palindrom\n"):printf("Not Palindrom\n"); } int check_palindrom(char *s) { int i,j; for(i=0;s[i];i++); for(i=i-1,j=0;i>j;i--,j++) if(s[i]!=s[j]) return 0; if(s[i]==s[j]) return 1; } 

输入字符串……

雷达

回文

你可以这样做:

 #include  #include  int check_palindrome(char string []); int main() { char string[20]; printf("Enter the string...\n"); scanf ("%s", &string); int check; check = check_palindrome (string); if (check == 0) printf ("Not Palindrome\n"); else printf ("Palindrome\n"); return 0; } int check_palindrome (char string []) { char duplicate []; strcpy (string, duplicate); strrev (string); if (strcmp (string, duplicate) == 0) return 1; else return 0; } 

这使用strcmpstrrev函数。

看看这段代码,就是我实现它的方式(记得#include 或者它不起作用):

 for(i = 0; i < string_length; i++) { if(sentence[i] == sentence[string_lenght-1-i]) palindrome = true; else { palindrome = false; break; } } 

这样做会检查你的句子是否是回文,并且在第一次出现时这不是真的,它将打破for循环。 你可以使用类似的东西

 if(palindrome) printf(..); else printf(..); 

为用户提供简单的提示。

示例:

雷达是回文

阿巴是回文

abcabc不是回文

请注意这个事实

阿巴

由于'A'和'a'具有不同的ASCII码,因此不被认为是回文:

'A'的值为65

根据ASCII表, 'a'的值为97。 你可以在这里找到更多。

您可以避免此问题将字符串的所有字符转换为小写字符。 您可以这样做,包括库并调用函数int tolower(int c); 像那样 :

 for ( ; *p; ++p) *p = tolower(*p); 

要么

 for(int i = 0; str[i]; i++){ str[i] = tolower(str[i]); } 

Earlz的代码,看一下这个Q&A ,深入研究它。

编辑:我做了一个简单的程序,看看它是否可以帮助你

 #include  #include  #include  #include  #include  void LowerCharacters(char *word, int word_lenth); int main(void){ char *word = (char *) malloc(10); bool palindrome = false; if(word == 0) { printf("\nERROR : Out of memory.\n\n"); return 1; } printf("\nEnter a word to check if it is palindrome or not : "); scanf("%s", word); int word_length = strlen(word); LowerCharacters(word,word_length); for(int i = 0; i < word_length; i++) { if(word[i] == word[word_length-1-i]) palindrome = true; else { palindrome = false; break; } } palindrome ? printf("\nThe word %s is palindrome.\n\n", word) : printf("\nThe word %s is not palindrome.\n\n", word); free(word); return 0; } void LowerCharacters(char *word, int word_length){ for(int i = 0; i < word_length; i++) word[i] = tolower(word[i]); } 

输入:

输入一个单词来检查它是否是回文:RadaR

输出:

雷达这个词是回文。