C中的回文程序

Palindrome program in C

我的回文C程序在函数中有错误。我的函数不比较字符串中的 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),计数器将从 0 变为 2,中间元素未选中。这是没有必要的,因为它是一个回文,它总是匹配自己

看看这段代码,这就是我实现它的方式(记得#include <stdbool.h>否则它不会工作):

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(..);

为用户提供简单的提示。

示例:

radar is palindrome

abba is palindrome

abcabc is not palindrome

请注意

Abba

不被识别为回文,因为 'A' 和 'a' 具有不同的 ASCII 码:

'A' has the value of 65

'a' has the value of 97 according to the ASCII table. You can find out more here.

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

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

or

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

Code by Earlz, take a look at this Q&A to look deeper into that.

编辑:我编写了一个简单的程序来执行此操作,看看它是否可以帮助您

#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include <ctype.h>

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]);
}

输入:

Enter a word to check if it is palindrome or not : RadaR

输出:

The word radar is palindrome.

#include<stdio.h>
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;
}

Enter the string...

radar

Palindrom

你可以这样做:

#include <stdio.h>
#include <string.h>

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 函数。

我以前在一本叫"Cracking the Coding Interview"的面试书上看过这个算法。

作者在其中展示了一个非常简单易行的代码实现。代码如下:另外 here 是解释代码的视频。

#include<stdio.h>
#include<string.h> // 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<stdio.h>
int main()
{
 char str[50];
 int i,j,flag=1;
 printf("Enter the string");
 gets(str);
 for(i=0;str[i]!='[=10=]';i++);
 for(i=i-1,j=0;j<i;j++,i--)
 {
    str[i]=str[i]+str[j];
    str[j]=str[i]-str[j];
    str[i]=str[i]-str[j];
 }
 for(i=0;str[i]!='[=10=]';i++);
 for(i=i-1,j=0;j<i;j++,i--)
 {
    if(str[i]==str[j]){
        flag=0;
        break;
    }
 }if(flag==0)
 {
    printf("Palindrome");
 }else
 { 
    printf("Not Palindrome");
 }

}