简体   繁体   English

C 中字符串的回文检查器

[英]Palindrome Checker for strings in C

I'm a new CS student and I was having trouble with my palindrome function.我是一名新的 CS 学生,我的回文函数有问题。 It works with single words perfectly fine and it also works with some multi-words, such as "race car", though not all.它可以很好地处理单个单词,也可以处理一些多单词,例如“race car”,尽管不是全部。 For my assignment, one of the words that I need to check is "dot i tod", though I can't get the palindrome to work properly.对于我的作业,我需要检查的单词之一是“dot i tod”,尽管我无法让回文正常工作。 I would appreciate any help!我将不胜感激任何帮助! (Assume only one space between words and all words are lowercase) (假设单词之间只有一个空格并且所有单词都是小写的)

EDIT: I realized that I should've put the beginning of my function, so I'll put that in now.编辑:我意识到我应该把函数的开头放在里面,所以我现在就把它放在里面。

char sentence[20] = "dot i tod";
int len = strlen(sentence);
int i = 0;
int end = len;
char left[i];
char right[end];
 while (i < len){
   printf("%c ", sentence[i]);
      printf("\n");
      printf("%c ", sentence[len - i - 1]);
      printf("\n");
   if (left[i] == right[end - i  - 1]){
     i++;
     end--;
   }
   if (left[i] == ' '){
     i++;
   }
   if (right[end] == ' '){
     end--;
   }
   if (left[i] != right[end]){
     printf("Not a palindrome");
     break;
   }
   if (i >= end){
     printf("Palindrome");
     break;
   }
 }
}

For starters these declarations首先,这些声明

char left[i];
char right[end];

are incorrect and do not make sense.是不正确的,没有意义。 For example you may not declare a variable length array with the size equal to 0.例如,您不能声明大小等于 0 的可变长度数组。

But if instead of the variables left and right (that is instead of uninitialized arrays) to use the variable sentence then nevertheless the logic of your program is incorrect.但是,如果不是变量leftright (即不是未初始化数组)使用的变量首句仍然程序的逻辑是不正确。

Let's assume that the string contains only one character for example 'A' .让我们假设字符串只包含一个字符,例如'A'

In this case the condition in the if statement evaluates to logical true在这种情况下,if 语句中的条件计算为逻辑真

if (sentence[i] == sentence[end - i - 1]){ i++; if (sentence[i] == sentence[end - i - 1]){ i++; end--;结尾 - ; } }

As a result after the increment of i it will point to the terminating zero '\\0' .因此,在i的增量之后,它将指向终止零'\\0' While after the decrement of end it will point to the first and single character 'A'.而在end递减之后,它将指向第一个和单个字符 'A'。

After that this if statement will evaluate also to the logical true之后这个 if 语句也将评估为逻辑真

if (sentence[i] != sentence[end]){ printf("Not a palindrome"); if (sentence[i] != sentence[end]){ printf("不是回文"); break;休息; } }

because now And the program outputs that the string is not a palindrome.因为现在 并且程序输出该字符串不是回文。

Always try to write a more generic function.始终尝试编写更通用的函数。

For example the function can look the following way.例如,该函数可以如下所示。

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

int is_palindrome( const char *s )
{
    const char *first = s, *last = s + strlen( s );
    int success = 1;
    
    do
    {
        while ( first != last && isspace( ( unsigned char )*first ) ) ++first;
        
        while ( last != first && isspace( ( unsigned char )*--last ) );

        if ( ( success = first != last && *first == *last ) ) ++first;
    } while ( success && first != last );
    
    return first == last;
}

int main(void) 
{
    const char *s1 = "race car";
    const char *s2 =  "dot i tod";
    const char *s3 = " 1 2 3 2 1 ";
    const char *s4 = " 1 2 33 2 1 ";
    const char *s5 = " 1 2 34 2 1 ";
    
    return 0;
}

The program output is程序输出是

"race car" is palindrome
"dot i tod" is palindrome
" 1 2 3 2 1 " is palindrome
" 1 2 33 2 1 " is palindrome
" 1 2 34 2 1 " is not palindrome

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM