代码之家  ›  专栏  ›  技术社区  ›  user3259144

正在检查回文

  •  0
  • user3259144  · 技术社区  · 11 年前

    下面的代码应该检查最多9个字符的回文,尽管我将数组设置为10以留出空值。问题是当命令屏幕出现以输入信息时,每次都会冻结。我不确定问题出在哪里,也确定代码中有一些错误,我没有注意到。感谢任何帮助。

    #include <stdio.h>
    #include <stdlib.h>
    /*Delare the prototypes*/
    char palindromes(char[], int, int);
    char backwards(char[], int, int);
    
    int main ()
    {
       const int arraysize= 10;/*Sets a size for the array*/
       char userinput[10];/*Declares the character array for the input*/
       int palindex= 0;/*Declares the index to check for a palindrome*/
       int index= 0;/*Index for the counting loop*/
       int counter= 0;/*Counts number of elements entered in the array*/
       int palcheck= 1;/*Sets a value for the palindrome checking loop, for verification*/
       int charswap= 0;/*Sets an index for the loop that swaps the values*/
       int printindex= 0;/*Index to print the values on the screen*/
    
       printf("Please enter a series of nine or less characters to test for a palindrome.\n");
       scanf(" %9s", &userinput);
       printf("\n");
    
       for(index; userinput[index] !=0; index++)
       {
          counter +=1;
       }/*End of array element counter*/
    
    
       for(palindex; palindex < (counter/2); palindex++)
       {
          palcheck= palindromes(userinput, counter, palindex);
          if(palcheck = 0)
          break;
       }/*End of palidrome verification loop*/
    
       if(palcheck = 0)
       {
          printf("Your input was not a palindrome \n");
       }/*End of if statement*/
       else
       {
          printf("Your input was a palindrome \n");
       }/*End of else statement*/
    
       for(palindex; palindex < (counter/2); palindex++)
       {
          backwards(userinput, counter, palindex);
       }/*End of reversing the array loop*/
    
       printf("Your input backwards is: ");
       for(printindex; printindex < counter; printindex++)
       {
          printf("%c", userinput[printindex]);
       }/*End of printing backwards loop*/
    }/*End of main function*/
    
    char palindromes(char userinput[], int counter, int palindex)
    {
       char firstchar;/*Temp variable for the two items to be tested*/
       char lastchar;/*Temp variable for the two iteams to be tested*/
       firstchar = userinput[palindex];
       lastchar = userinput[counter - palindex];
       if(firstchar = lastchar)
       {
          return 1;
       }
       else
       {
          return 0;
       }
    }/*End of palidrome function*/
    
    char backwards(char userinput[], int counter, int palindex)
    {
       char temp;/*Sets a temporary value to swap the two values*/
       temp = userinput[palindex];
       userinput[palindex] = userinput[counter-palindex];
       userinput[counter-palindex] = temp;
    }/*End of reverse function*/
    
    2 回复  |  直到 11 年前
        1
  •  2
  •   photoionized    11 年前

    有太多的错误让人记不住了,只是一行一行地去做,但这里有一些开始

    • 在所有要重写变量的if语句中,使用 == 而不是 =
    • 计数器没有执行任何操作,请使用scanf和%n查找读取的字符数
    • 您不希望将计数器变量作为中间参数传递给回文函数,因为输入字符串中的尾随“\0”。。。而是使用counter1

    可能还有其他版本,但这里是原始代码的最低工作版本:

    #include <stdio.h>
    #include <stdlib.h>
    /*Delare the prototypes*/
    char palindromes(char[], int, int);
    char backwards(char[], int, int);
    
    int main ()
    {
       char userinput[10];/*Declares the character array for the input*/
       int palindex= 0;/*Declares the index to check for a palindrome*/
       int index= 0;/*Index for the counting loop*/
       int counter= 0;/*Counts number of elements entered in the array*/
       int palcheck= 1;/*Sets a value for the palindrome checking loop, for verification*/
       int charswap= 0;/*Sets an index for the loop that swaps the values*/
       int printindex= 0;/*Index to print the values on the screen*/
    
       printf("Please enter a series of nine or less characters to test for a palindrome.\n");
       scanf(" %9s%n", &userinput, &counter);
       printf("\n");
       for(palindex; palindex < (counter/2); palindex++)
       {
          palcheck = palindromes(userinput, counter-1, palindex);
          if(palcheck == 0)
          break;
       }/*End of palidrome verification loop*/
    
       if(palcheck == 0)
       {
          printf("Your input was not a palindrome \n");
       }/*End of if statement*/
       else
       {
          printf("Your input was a palindrome \n");
       }/*End of else statement*/
    
       for(palindex; palindex < (counter/2); palindex++)
       {
          backwards(userinput, counter, palindex);
       }/*End of reversing the array loop*/
    
       printf("Your input backwards is: ");
       for(printindex; printindex < counter; printindex++)
       {
          printf("%c", userinput[printindex]);
       }/*End of printing backwards loop*/
       printf("\n");
    }/*End of main function*/
    
    char palindromes(char userinput[], int counter, int palindex)
    {
       char firstchar;/*Temp variable for the two items to be tested*/
       char lastchar;/*Temp variable for the two iteams to be tested*/
       firstchar = userinput[palindex];
       lastchar = userinput[counter - palindex];
       if(firstchar == lastchar)
       {
          return 1;
       }
       else
       {
          return 0;
       }
    }/*End of palidrome function*/
    
    char backwards(char userinput[], int counter, int palindex)
    {
       char temp;/*Sets a temporary value to swap the two values*/
       temp = userinput[palindex];
       userinput[palindex] = userinput[counter-palindex];
       userinput[counter-palindex] = temp;
    }/*End of reverse function*/
    
        2
  •  2
  •   Vinay    11 年前
    1. 空字符为 \0 但你正在检查 0 . userinput[index]!='\0'
    2. 第三天 for() 循环,palindex应设置为0,i,e for(palindex=0;....)

    用于反转字符串,而不是调用 backwards() 中每个字符i,e的函数 for() 循环,您可以将 for() 循环输入 向后() 函数和调用 向后() 主要只有一次。这将缩短执行时间。这适用于 palindrome() 功能也一样。