简体   繁体   English

如何将数组与反向数组进行比较并检查它们的值是否匹配?

[英]How to compare an array to a reversed one and check if their values match?

I wrote a program that reverses an array with the strrev() function and checks if its values matches the original one, sort of a palindrome. 我编写了一个用strrev()函数反转数组的程序,并检查它的值是否与原始数据匹配, strrev()回文。 When the values match, it prints Palindrome , else, Not a palindrome . 当值匹配时,它会打印Palindrome ,否则, Not a palindrome

But when I compare them, and the values don't match, it still prints Palindrome . 但是当我比较它们,并且值不匹配时,它仍会打印Palindrome

Here is the code: 这是代码:

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <string.h>

#define MAX_LEN 100

void palindrom(char string[]);

int main()
{
    char string[MAX_LEN] = { 0 };

    printf("Enter string (max length 100 chars): ");
    fgets(string, MAX_LEN, stdin);
    if(string[strlen(string)-1] == '\n') { string[strlen(string)-1] = 0; }
    palindrom(string);

    return (0);
} 

void palindrom(char string[])
{
    int check = 0;
    check = strcmp(strrev(string), string);
    if (check == 0)
    {
        printf("Palindrome");
    }
    else
    {
       printf("Not a palindrome");
    }
}

What's my problem? 我的问题是什么? Thanks. 谢谢。

从我可以告诉strrev也可以修改原始字符串,所以你需要复制它。

The key is strrev . 关键是strrev

Here's a program in C that will do what you're testing for: 这是C中的一个程序,可以执行您正在测试的内容:

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

int main()
{
   char a[100], b[100];

   printf("Enter the string to check if it is a palindrome\n");
   fgets(a, 100, stdin);

   strcpy(b,a);
   strrev(b);

   if (strcmp(a,b) == 0)
      printf("Entered string is a palindrome.\n");
   else
      printf("Entered string is not a palindrome.\n");

   return 0;
}

Since others have clarified what the problem is, I would like to point that it would be faster to check if s[0] == s[len-1], s[1] == s[len-2], until half (rounded up) of the string has been checked. 由于其他人已经澄清了问题所在,我想指出检查s [0] == s [len-1],s [1] == s [len-2]是否会更快,直到一半已经检查了字符串的(四舍五入)。

This would require no extra memory, no copy and half as many comparisons. 这将不需要额外的内存,没有副本和一半的比较。 Something along the lines of: 有点像:

void palindrom(char string[])
{
    int len = strlen(string) - 1;
    int i, limit = len/2 + (len % 2);
    for (i = 0; i < limit; i++){
        if (string[i] != string[len-i]){
            printf("Not a palindrome\n");
            return;
        }
    }
    printf("Palindrome\n");
}

Your function fails because strrev modifies the string. 您的函数失败,因为strrev修改了字符串。 You effectively always compare the reversed string to itself. 您实际上总是将反向字符串与自身进行比较。

Here is an alternate function that does not modify the string: 这是一个不修改字符串的备用函数:

void palindrom(const char *str) {
    for (size_t i = 0, j = strlen(str); i < j; i++, j--) {
        if (str[i] != str[j - 1]) {
            printf("Not a palindrome\n");
            return;
        }
    }
    printf("Palindrome\n");
}

You don't need to use strrev to test for a palindrome the following function detects a palindrome just fine without using non-standard C functions: 您不需要使用strrev来测试回文,以下函数在不使用非标准C函数的情况下检测到回文就好了:

int ispalindrome(char *str, int len)
{   
    char *p = &str[0];
    char *q = &str[len - 1];
    do
    {
        if(p >= q)
        {
            return 1;
        }
    } while (*p++ == *q--);
    return 0;
}

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

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