Program that tells you whether what you enter is a palindrome or not
#include <stdio.h>
#include <string.h>
#define N 100
#define PALINDROME 0
#define NONPALINDROME 1
/*Program that tells you whether what you enter is a palindrome or not*/
char pal[N]; //input line
int i; //counter
int k; //counter
int tag;
int flag = PALINDROME; /*flag=0 ->palindrome, flag =1 ->nonpalindrome*/
int main()
{
printf("Enter something: \n");
scanf("%s", pal);
tag = strlen(pal); /*determine length of string*/
/* pointer running from the beginning and the end simultaneously
looking for two characters that don't match */
/* initially assumed that string IS a palindrome */
/* the following for loop looks for inequality and flags the string as
a non palindrome the moment two characters don't match */
for (i=0,k=tag-1; i=0; i++,k--) {
if(pal[i] != pal[k]) {
flag=NONPALINDROME;
break;
}
}
if(flag == PALINDROME) {
printf("This is a palindrome\n");
}
else {
printf("This is NOT a palindrome\n");
}
return 0;
}
Hiç yorum yok:
Yorum Gönder