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?