Palindrome in C (Please try my code)

  • Thread starter Thread starter Kontilera
  • Start date Start date
  • Tags Tags
    Code
Click For Summary

Discussion Overview

The discussion revolves around creating a C program to check if one or multiple words are palindromes. Participants are sharing code attempts and seeking guidance on issues encountered during implementation.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant expresses difficulty with their compiler and seeks help with their code, suspecting misuse of the strtok() function.
  • Another participant points out that strtok() should be called with a comma as the separator and suggests using a pointer for the word variable.
  • Concerns are raised about an infinite loop due to the way the word variable is used, as it cannot be reassigned in the while loop.
  • A participant acknowledges mistakes in their code translation and proposes a revised version that uses a pointer to handle strtok() correctly.
  • Another participant identifies syntax and semantic errors in the revised code, specifically regarding variable assignments and loop behavior.
  • A question is posed about handling input longer than the defined character array size, raising concerns about potential buffer overflow.

Areas of Agreement / Disagreement

Participants generally agree on the identification of errors in the code, but there is no consensus on a fully corrected version or approach to handle all edge cases.

Contextual Notes

Limitations include unresolved issues regarding buffer sizes and the handling of input longer than expected, as well as the need for proper variable assignments and loop control.

Who May Find This Useful

Readers interested in C programming, particularly those working on string manipulation and palindrome checking algorithms.

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?
 

Similar threads

  • · Replies 21 ·
Replies
21
Views
3K
Replies
7
Views
2K
  • · Replies 21 ·
Replies
21
Views
4K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
Replies
8
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
Replies
1
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K