检查回文

时间:2014-04-08 04:01:46

标签: c function palindrome

以下代码应该检查最多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 个答案:

答案 0 :(得分:2)

真的记得有太多错误,只是一行一行,但这里有一些初学者

  • 在您覆盖变量的所有if语句中,使用==代替=
  • 你的计数器什么都不做,使用带有%n的scanf来查找读取字符的数量
  • 您不希望将计数器变量作为中间参数传递给您的回文函数,因为输入字符串中的尾部为'\ 0'而是使用counter-1

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

#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*/

答案 1 :(得分:2)

  1. 空字符为\0,但您正在检查0userinput[index]!='\0'
  2. 在第3个for()循环中,palindex应设为0,i,e for(palindex=0;....)
  3. 对于反转字符串,而不是为backwards()循环中的每个字符i调用for()函数,您可以将for()循环放在backwards()函数中并调用{ {1}}只在主要一次。这将改善执行时间。这也适用于backwards()函数。