# Palindrome Algorithm in C .

• 01-21-2013
Mr.Lnx
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?
• 01-21-2013
Salem
> How many pass algorithm does? 1 + 1/2 or 2?
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.
• 01-22-2013
Mr.Lnx
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 ?
• 01-22-2013
Salem
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.