Thread: Palindrome Algorithm in C .

  1. #1
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357

    Palindrome Algorithm in C .

    Hello to all. I have some confusion here.....

    My code :

    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdbool.h>
    #include <ctype.h>
    #define STR_LEN 50
    int is_palindrome( const char []);
    void read_line(char [] , int );
    int main( void )
    {
        char str[STR_LEN + 1] = {'\0'};
        
        for(;;) {
        
        printf("\n Enter a word (Enter a single letter to terminate) : \n");
        read_line(str , STR_LEN );  
        
        if(strlen(str) < 2)
        break;
        
        if( is_palindrome( str ) )
        printf(" Word %s is  palindrome " , str);
        
        else
        printf(" Word %s is not palindrome " , str);
    }
            
            return 0;
    }
    void read_line(char str[] , int n)
    {
        int ch , i=0;
        while( (ch = getchar()) != '\n' ) {
            
            if( !isalpha(ch)) {
            printf(" \n *Rejected* '%c' is not a letter " , ch);
            continue;
    	    }    
            
            if(i < n )
            str[i++] = ch;
        }
            
            str[i] = '\0';
    }
    
    int is_palindrome(const char* str) {
    	
        const char* last = str + strlen(str) - 1; 
        while (str < last) {
            if (*str++ != *last--) return 0;
        }             
     
        return 1;
    }
    I have a basic question not about the code but about the algorithm which code uses in order to find a palindrome.

    How many pass algorithm does? 1 + 1/2 or 2?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > How many pass algorithm does? 1 + 1/2 or 2?
    How about adding some code to find out?
    Code:
    int is_palindrome(const char* str) {
        int numPasses = 0;
        const char* last = str + strlen(str) - 1;
        while (str < last) {
            numPasses++;
            if (*str++ != *last--) return 0;
        }            
      
        return 1;
    }
    Printing numPasses is an exercise for you.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357
    I meant the passes from complexity perspective. For example....

    str pointer will pass 1 and 1/2 times the list (the word) one time when we read the word and one half time when we look for the characters ....
    last pointer will pass 1/2 times the list (the word). My question (if it makes sense) : The considerable pass from this process are 1 + 1/2 or 2?

    I have read that if we have this list : 0 1 2 3 4 5 6 7 8 9 10

    we need n/2 pass in order to find some element . The total pass is n/2 or n/2 + n/2 ?
    Last edited by Mr.Lnx; 01-22-2013 at 04:16 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you've got one process step which takes n iterations, followed by another which takes n as well, then the overall order of the computation is still n.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Palindrome in C
    By DaniiChris in forum C Programming
    Replies: 13
    Last Post: 07-24-2008, 11:32 PM
  2. palindrome
    By chodmama in forum C Programming
    Replies: 1
    Last Post: 02-20-2006, 02:52 AM
  3. Palindrome
    By Ginny Morgan in forum C Programming
    Replies: 7
    Last Post: 05-08-2003, 04:04 PM
  4. Palindrome
    By Nicholas35 in forum C++ Programming
    Replies: 7
    Last Post: 02-07-2002, 10:06 PM
  5. Palindrome
    By a_learner in forum C Programming
    Replies: 5
    Last Post: 11-30-2001, 09:03 AM