1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Palindrome in C (Please try my code!)

  1. Oct 12, 2012 #1
    1. The problem statement, all variables and given/known data
    Make a code that can check if one or many words are palindromes.



    3. The attempt at a solution
    I cant get my compiler to work so I wrote my attempt in a note document. Please can you check if it works and if it doesnt 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 seperate 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;
     
     
  2. jcsd
  3. Oct 12, 2012 #2

    Mark44

    Staff: Mentor

    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 (Text):
    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 (Text):
    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 (Text):
    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).
     
  4. Oct 12, 2012 #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;
     
  5. Oct 12, 2012 #4

    Mark44

    Staff: Mentor

    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: Oct 12, 2012
  6. Oct 12, 2012 #5

    jbunniii

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member

    What if I type a word with 101 characters in it?
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Palindrome in C (Please try my code!)
  1. Basic C++ Coding? (Replies: 3)

  2. Simple C coding (Replies: 14)

Loading...