简体   繁体   English

回文C程序混乱

[英]Palindrome C program confusion

I am exploring working with strings and I have some questions about this particular program. 我正在探索使用字符串的方法,对此特定程序有一些疑问。 Could someone help explain why and how the x < i/2 as well as the word[i - 1 - x] works. 有人可以帮助解释x < i/2以及word[i - 1 - x]方式。

Why does it have to be i/2 ? 为什么必须是i/2
Why does it have to be word[i - 1 - x] ? 为什么必须是word[i - 1 - x]

#include <stdio.h>


int main()
{
     char word[15];
     int i, x;

    printf("Enter a word\n");
    scanf("%s", word);

    for(i = 0; word[i] != '\0';)
        i = i + 1;


    for(x = 0; x< i/2; x++)

        if(word[x] != word[i-1-x])
        {printf("Your word is not a palindrome\n");}
        else
        {
            printf("Your word is a palindrome\n");
        }


}

You're comparing half the string. 您正在比较字符串的一半。 So for i/2 you're looking at the first half of the string and for word[i - 1 - x] your looking ahead at the "mirror" position of the other half of the string. 因此,对于i/2您正在查看字符串的前半部分,而对于word[i - 1 - x]您正在向前查看字符串的另一半的“镜像”位置。

Why does it have to be i/2 ? 为什么必须是i/2

It does not have to be this way: i would work too, but it would not be optimal . 不一定要这样: i也可以工作,但不是最佳选择 If you have checked all character pairs going at it from both ends, and the letters are the same up to the middle of the word, the rest of the checks will succeed as well. 如果您已经检查了所有从两端到它的字符对,并且直到单词中间的字母都是相同的,那么其余的检查也将成功。

Consider for example the word "alula" . 考虑例如单词"alula" You start by comparing the initial a to the last a , then you compare l in the second position to l in the second position from the back, then you compare u to itself. 您可以通过最初的比较开始a到最后a ,然后你比较l在第二位置,以l在从后面的第二位置,那么你比较u本身。 Now you've reached the middle of the word at i/2 . 现在您已经到达i/2的单词中间。 If you continue, you would compare the same pairs of characters, but you would pick them from different indexes. 如果继续,则将比较相同的字符对,但是将从不同的索引中选择它们。 It is guaranteed that all these pairs are going to match, so you can save yourself some CPU cycles by skipping them altogether. 确保所有这些对都将匹配,因此您可以通过完全跳过它们来节省一些CPU周期。

Why does it have to be word[i - 1 - x] ? 为什么必须是word[i - 1 - x]

Because arrays are zero-based. 因为数组是从零开始的。 i-1 is the index of the last character, hence i - 1 - x is the x -th index counting from the back. i-1是最后一个字符的索引,因此i - 1 - x是从背面开始计数的第x个索引。

I am assuming that you have stored the string in a variable named string in the below code, this is the main logic of the function 我假设您已在以下代码中将字符串存储在名为string的变量中,这是该函数的主要逻辑

NOTE : you have iterate the for loop for legth/2 times also and it will give the same results 注意:您也将for循环进行了legth / 2次的迭代,它将得到相同的结果

  #include<stdio.h>
  #include<string.h>
  int main()  
    {
        int i;
        int flag=0;
        for(i=0;i<strlen(string);i++)
        {
            if(string[i]==string[strlen(string)-i-1])
            flag=1;
            else
            break;
        }
        if(flag==1)
        printf("palindrome string");
        else
        printf("not palindrome string");

        return 0;
}

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

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