简体   繁体   English

c - 反向字符串比较(回文)

[英]c - reverse string comparison (palindrome)

I want to check an entered text for a palindrome.我想检查输入的文本是否有回文。

However, when I enter a palindrome, I always get that it is NOT a palindrome.但是,当我输入回文时,我总是知道它不是回文。 Have I done something wrong with the stored linefeed character?我对存储的换行符做错了吗?

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

int main()
{
char text[256], reverse[256];
int i, j;

    printf("Type a text: ");
    fgets(text, 255, stdin);

    j = strlen(text)-1;
    
    for (i=0; i<=j; ++i)
        {
            if(text[i] >= 'A' && text[i] <= 'Z')
            {
            text[i] += 32;
            }
            if(text[i] == '\n')
            {
            text[i] = i - 1;
            }
        }

    i = strlen(text)-1;

    for (i = i, j = 0 ;i >= 0 ; --i, ++j)
        {
        reverse[j] = text[i];
        }

    printf("Text: %s\n", text);
    printf("Reverse: %s\n", reverse);

    if (strcmp(text, reverse) == 0)
        {
        printf("The entered text \"%s\" is a palindrome!\n", text);
        }
    else
        {
        printf("The entered text \"%s\" is NOT a palindrome!\n", text);
        }
}

Yes, your treatment of the linefeed character is wrong.是的,您对换行符的处理是错误的。

You replaced the linefeed character with some other (or maybe same) character, but it means there will be an extra character in the string and the extra character will prevent the string from being jugded as a palindrome.您将换行符替换为其他(或可能相同的)字符,但这意味着字符串中会有一个额外的字符,并且额外的字符将防止字符串被判断为回文。

You should replace the linefeed character to '\0' (NUL character), which is used as string termination mark in C.您应该将换行符替换为'\0' (NUL 字符),它在 C 中用作字符串终止标记。

This if statement这个 if 语句

      if(text[i] == '\n')
        {
        text[i] = i - 1;
        }

does not make a sense.没有意义。 Moreover as the new line character '\n' is a single character in an entered string then keeping it within the string will result that the string is not a palindrome.此外,由于换行符'\n'是输入字符串中的单个字符,因此将其保留在字符串中将导致该字符串不是回文。

You need to substitute it for the zero character '\0' .您需要用它代替零字符'\0'

And the array reverse does not contain a string after this loop并且数组reverse在此循环之后不包含字符串

for (i = i, j = 0 ;i >= 0 ; --i, ++j)
    {
    reverse[j] = text[i];
    }

Also using the function strlen is redundant and inefficient.同样使用 function strlen是多余且低效的。

The program can look the following way.该程序可以如下所示。

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

int main(void) 
{
    char text[256], reverse[256];

    printf( "Type a text: " );
    
    fgets( text, sizeof( text ), stdin );
    
    size_t n = 0;
    
    while ( text[n] != '\0' )
    {
        if ( text[n] == '\n' )
        {
            text[n] = '\0';
        }
        else
        {
            text[n] = tolower( ( unsigned char )text[n] );
            ++n;
        }
    }
    
    reverse[n] = '\0';

    for ( size_t i = 0; n-- != 0; i++ )
    {
        reverse[n] = text[i];
    }
    
    printf( "Text: \"%s\"\n", text );
    printf( "Reverse: \"%s\"\n", reverse);

    if ( strcmp( text, reverse ) == 0 )
    {
        printf( "The entered text \"%s\" is a palindrome!\n", text );
    }
    else
    {
        printf( "The entered text \"%s\" is NOT a palindrome!\n", text );
    }
    
    return 0;
}

The program output might look like程序 output 可能看起来像

Type a text: ABC cba
Text: "abc cba"
Reverse: "abc cba"
The entered text "abc cba" is a palindrome!

In general the used by you approach when you need an auxiliary array and when the source string is being changed is not good.通常,当您需要辅助数组并且更改源字符串时,您使用的方法并不好。

Your could write a simple function as it is shown in this demonstrative program.您可以编写一个简单的 function,如演示程序所示。

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

int is_palindrome( const char *s )
{
    size_t n = strlen( s );
    size_t i = 0;
    
    while ( i < n / 2 && tolower( ( unsigned char )s[i] ) == 
                         tolower( ( unsigned char )s[n -i -1] ) )
    {
        ++i;
    }
    
    return i == n / 2;
}

int main(void) 
{
    char text[256];

    printf( "Type a text: " );
    
    fgets( text, sizeof( text ), stdin );
    
    text[ strcspn( text, "\n" ) ] = '\0';

    printf( "Text: \"%s\"\n", text );

    if ( is_palindrome( text ) )
    {
        printf( "The entered text \"%s\" is a palindrome!\n", text );
    }
    else
    {
        printf( "The entered text \"%s\" is NOT a palindrome!\n", text );
    }
    
    return 0;
}

The program output might look like程序 output 可能看起来像

Type a text: ABC cba
Text: "ABC cba"
The entered text "ABC cba" is a palindrome!

As you see the original string was not changed except the new line character '\n' was substituted for the zero character '\0' .如您所见,原始字符串没有更改,只是用新行字符'\n'替换了零字符'\0'

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

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