Palindrome example with C programming

#include<stdio.h>

char lower(char c){
	if('A' <= c && 'Z' >= c)
		return (c + 32);
	return c;
}

int alpha(char c){
	c = lower(c);
	return (('a' <= c && 'z' >= c));
}

int palindrome(char *str){
	char *head = str, *tail = str;
	while(*tail) tail ++;
	tail --;
	while(head <= tail){
		if(!alpha(*head)){
			head ++;
			continue;
		}
		if(!alpha(*tail)){
			tail --;
			continue;
		}
		if(lower(*head) != lower(*tail))
			break;
		head ++;
		tail --;
	}
	return (tail < head);
}

void main(){
	char sentence[200];
	printf("Enter a message: ");
	fflush(stdin);
	gets(sentence);
	if(palindrome(sentence)){
		printf("Palindrome\n");
		return;
	}
	printf("Not a palindrome\n");
}
Download

Comments

Popular posts from this blog