Thread: palindromes in a string

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    7

    palindromes in a string

    Hi
    I'm trying to finish up this program but it won't work and I don't see what I'm doing wrong ...

    I have a string input from the user, I'll use string tokenization to split the sentence into words

    Code:
    #include <string.h>
    #include <stdio.h>
    #define SIZE 80
    int check(char string[], int size);
    int main() {
        char a[SIZE];
        int count=0, l;
        char delims[] = " ";
        char *token;
    
        printf("Enter a sentence to check for palindromes\n> ");
        gets(a);
        token = strtok(a,delims);
        
        while(token != NULL) {
                    l=strlen(token)-1;
                    puts(token);
                    token = strtok(NULL, delims);
                    if(check(token,l) == 1)
                                      count++;
                                           }
        printf("Number of palindroms is: %d\n", count);
        system("pause");
        return 0;
    }
    int check(char string[], int size) {
        int i,flag=0;
        char temp[size];
        if(size <= 1)
                return 0;
    
       for(i=0;i<size;i++)
       string[i] = tolower(string[i]);
       strcpy(temp,string);
       strrev(temp);
       if(strcmp(string,temp)==0)
             return 1;
        
    }
    }
    Thank you

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Your spacing is uneven. You have an extra brace at the end.

    l is a bad variable name since it looks like a 1. Try len. (Although why doesn't check do it's own strlen call if it needs the length?)

    Your main error is that you've placed the call to strtok that's in the loop in the wrong position. It should be the last statement in the loop body.

    You also need a return 0 at the end of check. Otherwise, what happens if strcmp doesn't return zero?

    Overall, it seems a pretty inefficient implementation. You don't actually need another copy of the string.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    7
    Thank you very much!! that solved my problem!
    but can you explain to me how to do it without copying the string?

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    The idea is to have two pointers (or indices), one starting at the beginning of the string and the other at the end. While the two indicated chars are the same, move the pointers/indices towards each other (increment the low one, decrement the high one) and check again. If you find different chars, return 0 (false); if the pointers/indices "meet in the middle", return 1 (true).
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Palindromes
    By jewelz in forum C++ Programming
    Replies: 57
    Last Post: 02-23-2009, 06:11 PM
  2. Palindromes
    By jestervr in forum C Programming
    Replies: 8
    Last Post: 09-28-2008, 12:59 AM
  3. palindromes....
    By imbecile in C in forum C Programming
    Replies: 8
    Last Post: 08-09-2003, 05:08 PM
  4. Palindromes
    By cman in forum C Programming
    Replies: 1
    Last Post: 05-05-2003, 06:39 AM
  5. checking palindromes
    By jfoote in forum C Programming
    Replies: 2
    Last Post: 10-11-2002, 12:20 PM