Palindrome in C (Please try my code)

  • Thread starter Thread starter Kontilera
  • Start date Start date
  • Tags Tags
    Code
AI Thread Summary
The discussion focuses on troubleshooting a C program designed to check if words are palindromes. The original code has several issues, including incorrect usage of strtok() for word separation and syntax errors like undeclared variables and infinite loops. Suggestions include using a pointer for strtok() and correcting the assignment of words. The importance of proper memory management and ensuring that the loop iterates correctly based on input is emphasized. Overall, the conversation highlights common pitfalls in C programming and provides guidance for resolving them.
Kontilera
Messages
176
Reaction score
24

Homework Statement


Make a code that can check if one or many words are palindromes.



The Attempt at a Solution


I can't get my compiler to work so I wrote my attempt in a note document. Please can you check if it works and if it doesn't give me some guidence.. I have put a lot of time into the problem and now I thought I was finished but my computer stops working. :cry:

I suspect that I have missused strtok() but "," should separate the words.
Thanks.

PHP:
#include <stdio.h>
#include <string.h>
 
main()
{
   i=1;
   char words[100], invers[100], word[50];		
 
   printf("Please write your words:\n");
   gets(words);					
 
   word= strtok(words, ,);
while(word != NULL){

   strcpy(invers,word); 				
   strrev(invers);				
 
   if( strcmp(word,invers) == 0 )		
      printf("%s - Palindrome!\n", ord );
   else
      printf("%s Not Palindrome!\n", ord );}

   return 0;
 
Physics news on Phys.org
Kontilera said:

Homework Statement


Make a code that can check if one or many words are palindromes.

The Attempt at a Solution


I can't get my compiler to work so I wrote my attempt in a note document. Please can you check if it works and if it doesn't give me some guidence.. I have put a lot of time into the problem and now I thought I was finished but my computer stops working. :cry:

I suspect that I have missused strtok() but "," should separate the words.
Thanks.

PHP:
#include <stdio.h>
#include <string.h>
 
main()
{
   i=1;
   char words[100], invers[100], word[50];		
 
   printf("Please write your words:\n");
   gets(words);					
 
   word= strtok(words, ,);
while(word != NULL){

   strcpy(invers,word); 				
   strrev(invers);				
 
   if( strcmp(word,invers) == 0 )		
      printf("%s - Palindrome!\n", ord );
   else
      printf("%s Not Palindrome!\n", ord );}

   return 0;

As you suspected, you aren't using strtok correctly. If you want to specify that the separator is a comma, call it like this:
Code:
pChar = strtok(words, ",");
Here, pChar is declared as a pointer to char; i.e., char * pChar;

Here's a link to some documentation: http://www.cplusplus.com/reference/clibrary/cstring/strtok/

You have several other problems.
1. In this code,
Code:
word= strtok(words, ,);
the word variable cannot appear on the left side of an assignment statement. Since it is declared as an array of char, its value (an address) is constant. That's why I used a pointer in my code above.
2. In this code,
Code:
while(word != NULL)
you will have an infinite loop, since word never becomes null (as already mentioned, its value is constant).
3. There are a few syntax errors, such as missing brace at end of main function, and an undeclared variable (ord).
 
Ah thanks Mark44! Yeah, I see that I made some misstakes when I tried to translate the code to english for you. :frown:

Ah strtok returns a pointer.. maybe this would work then:
PHP:
#include <stdio.h>
#include <string.h>
 
main()
{
   char words[100], invers[100], word[40], *pchar;		
 
   printf("Please type your words:\n");
   gets(words);				
 
   pchar = strtok(words, ",");
while(pchar != NULL){
   word = *pchar
   strcpy(invers,word); 				
   strrev(invers);				
 
   if( strcmp(word,invers) == 0 )		
      printf("%s - Palindrome!\n", word );
   else
      printf("%s Not Palindrome! \n", word );}
}
   return 0;
 
I see a couple of problems with this attempt, one of which would generate a compile-time error.
1. Syntax error. word cannot appear on the left side of an assignment statement. This will result in a compiler error. To fix it, use strcpy to copy the characters pointed to by pchar to the word array.
2. Semantic error. Your loop will either run 0 times (if there is no comma in words) or forever. Assuming you found a comma in words, pchar is set with that address. The next iteration of the loop, pchar still has the same address, so the loop runs again. And so on.
 
Last edited:
What if I type a word with 101 characters in it?
 
Back
Top