Question

The following code is supposed to check for palindromes of up to nine characters, though I set the array to ten to leave space for the null. The problem is when the command screen comes up to enter the information, is freezes every time. I am not sure where the problem lies, and am also sure that there are errors in the code that I haven't been able to notice. Any help is appreciated.

#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*/
Was it helpful?

Solution

There were too many things wrong to really remember, just go line by line, but here are some starters

  • in all of your if statements you're overwriting your variables, use == instead of =
  • your counter is doing nothing, use scanf with %n to find the number of read characters
  • you don't want to pass in the counter variable to your palindromes function as the middle argument because of the trailing '\0' in your input string... instead use counter-1

there might have been others, but here's a minimally working version of your original code:

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

OTHER TIPS

  1. Null character is \0 but you are checking with 0. userinput[index]!='\0'
  2. In the 3rd for() loop, palindex should be set to 0, i,e for(palindex=0;....)

For reversing the string, instead of calling backwards() function for each character i,e in for() loop, you can put for() loop in backwards() function and call backwards() in main only once. This will improve the execution time. This applies to palindrome() function as well.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top