【问题标题】:A program to find out if a word is palindrome一个程序来判断一个单词是否是回文
【发布时间】:2020-04-26 19:11:32
【问题描述】:

编写一些算法来找出给定的单词是否是回文。但是我的一个变量(counter)在我调试时似乎没有更新,我无法弄清楚它有什么问题。我可能是错的......因为我不想盲目地在线复制一些代码,所以需要任何帮助。 下面是代码:

#include <iostream>
#include <cstring>


using namespace std;

int main(){
    //take input
    string input;
    cout << "Enter your word: ";
    cin >> input;

    //initialize arrays and variables
    int counter = 0, k = 0;
    int char_length = input.length();
    char characters[char_length];
    strcpy(characters, input.c_str());//copy the string into char array

    //index of character at the midpoint of the character array
    int middle = (char_length-1)/2;
    int booleans[middle]; //to keep 1's and 0's

    //check the characters
    int m = 0, n = char_length-1;
    while(m < middle && n > middle){
        if(characters[m] == characters[n]){
                booleans[k] = 1;
            } else {
                booleans[k] = 0;
            }
            k++;
            m++;
            n--;
    }

    //count number of 1's (true for being equal) in the booleans array
    for(int i = 0; i < sizeof(booleans)/sizeof(booleans[0])-1; i++){
        counter += booleans[i];
    }

    //compare 1's with size of array
    if(counter == middle){
        cout << input << " is a Palindrome!" << endl;
    } else {
        cout << input << " is not a Palindrome!" << endl;
    }

    return 0;
}

【问题讨论】:

    标签: c++ arrays algorithm variables palindrome


    【解决方案1】:

    兄弟,您似乎很难理解您的问题是什么以及您正在输入什么代码。我不是很有经验,但根据我的说法,回文是一个非常简单易行的程序,我会这样写:

    #include <iostream>
    #include <string.h>
    using namespace std;
    int main()
    {
        char str1[20], str2[20];
        int i, j, len = 0, flag = 0;
        cout << "Enter the string : ";
        gets(str1);
        len = strlen(str1) - 1;
        for (i = len, j = 0; i >= 0 ; i--, j++)
            str2[j] = str1[i];
        if (strcmp(str1, str2))
            flag = 1;
        if (flag == 1)
            cout << str1 << " is not a palindrome";
        else
            cout << str1 << " is a palindrome";
        return 0;
    }
    

    它适用于您可以尝试的所有情况。

    【讨论】:

    • 这也是一种方法,但我猜 OP 想知道他的代码有什么问题。
    【解决方案2】:

    如果你得到一个不匹配的结果,即(characters[m] == characters[n]) 是假的,那么你就没有回文。您可以在此时中断循环,返回 false 作为结果。您不这样做,而是在结果已知时进行测试。我会做类似的事情:

    // Check the characters.
    int lo = 0;
    int hi = char_length - 1;
    int result = true;  // Prefer "true" to 1 for better readability.
    while (lo < hi) {   // Loop terminates when lo and hi meet or cross.
      if(characters[lo] != characters[hi]) {
        // Mismatched characters so not a palindrome.
        result = false;
        break;
      }
      lo++;
      hi--;
    }
    

    我做了一些风格上的改进并清理了逻辑。您为解决问题做了大量工作。

    顺便说一句,您不需要检查两个指针​​lo 和hi 何时相等,因为它们都指向具有奇数个字母的单词的中间字符。由于该字符必须等于自身,因此无需测试。因此循环条件中的&lt; 而不是&lt;=。

    【讨论】:

      【解决方案3】:

      由于

      ,现有代码不适用于奇数长度的回文

      for(int i = 0; i &lt; sizeof(booleans)/sizeof(booleans[0])-1; i++)

      使用i&lt;=sizeof(booleans)/sizeof(booleans[0])-1; 或i&lt;sizeof(booleans)/sizeof(booleans[0]);。

      目前,您还没有计算character[middle-1] 和character[middle+1] 的比较。

      对于偶数长度的回文数,你将不得不稍微改变你的逻辑,因为偶数长度的回文数没有定义的中间点。

      #include <iostream>
      #include <cstring>
      
      
      using namespace std;
      
      int main(){
          //take input
          string input;
          cout << "Enter your word: ";
          cin >> input;
      
          //initialize arrays and variables
          int counter = 0, k = 0;
          int char_length = input.length();
          char characters[char_length];
          strcpy(characters, input.c_str());//copy the string into char array
      
          //index of character at the midpoint of the character array
          int middle = (char_length+1)/2;
          int booleans[middle]; //to keep 1's and 0's
      
          //check the characters
          int m = 0, n = char_length-1;
          while(m<=n){
              if(characters[m] == characters[n]){
                      booleans[k] = 1;
                  } else {
                      booleans[k] = 0;
                  }
                  k++;
                  m++;
                  n--;
          }
      
          //count number of 1's (true for being equal) in the booleans array
          for(int i = 0; i < sizeof(booleans)/sizeof(booleans[0]); i++){
              counter += booleans[i];
          }
          cout<<counter<<" "<<middle<<endl;
          //compare 1's with size of array
          if(counter == middle){
              cout << input << " is a Palindrome!" << endl;
          } else {
              cout << input << " is not a Palindrome!" << endl;
          }
      
          return 0;
      }
      

      这里布尔数组的大小是(length+1)/2,

      对于像abcba 这样的字符串,它的长度为3。

      这对应于a a、b b 和c c 之间的比较。由于中间元素相同,因此该情况​​的条件始终为真。

      而且去掉了中间的概念,要求指针移动,直到相互交叉。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-10-14
        • 2023-03-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多