Palindrome in C (Please try my code)

  • Thread starter Kontilera
  • Start date
  • Tags
    Code
In summary: Please type your words:\n"); gets(words); pchar = strtok(words, ",");while(pchar != NULL){ word = *pchar; strcpy(invers,word); strrev(invers); if( strcmp(word
  • #1
Kontilera
179
23

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
  • #2
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).
 
  • #3
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;
 
  • #4
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:
  • #5
What if I type a word with 101 characters in it?
 

1. What is a palindrome in C?

A palindrome in C is a word, phrase, or sequence of characters that reads the same backward as forward. For example, "mom," "racecar," and "madam" are all palindromes.

2. How can I check if a string is a palindrome in C?

You can check if a string is a palindrome in C by comparing the first and last characters, then the second and second-to-last characters, and so on until you reach the middle of the string. If all the characters match, the string is a palindrome. You can also reverse the string and compare it to the original string.

3. How does your code check for palindromes in C?

My code for checking palindromes in C first calculates the length of the string using the strlen() function. Then, it uses a loop to compare the first and last characters, and continues to do so until the middle of the string is reached. If the characters do not match, the string is not a palindrome.

4. Can your code handle strings with spaces and punctuation?

Yes, my code can handle strings with spaces and punctuation. It first removes all non-alphanumeric characters from the string using the isalnum() function, and then proceeds to check for palindromes.

5. Are there any limitations to your code for checking palindromes in C?

One limitation of my code is that it only works for ASCII characters. It may not work for strings with non-ASCII characters, such as accented letters or symbols. Additionally, the code may not work efficiently for very long strings, as it has to iterate through the entire string to check for palindromes.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
953
  • Engineering and Comp Sci Homework Help
Replies
2
Views
896
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
21
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
19
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
839
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
11
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
Back
Top