TDT4258/ex1/palindrome.c

30 lines
585 B
C
Raw Permalink Normal View History

2022-12-19 18:06:38 +01:00
#include <stdio.h>
int isValidCharacter(char character)
{
return ('a' <= character && character <= 'z')
|| ('A' <= character && character <= 'Z')
|| ('0' <= character && character <= '9');
}
int isPalindrome(int stringlen, char* string)
{
int m = 0;
int n = stringlen - 1;
while (m <= n) {
if (!isValidCharacter(string[m])) m++;
else if (!isValidCharacter(string[n])) n--;
else if (string[m] != string[n]) return 0;
else { m++; n--; }
}
return 1;
}
int main(int argc, char* argv[])
{
printf("Palindrome 1 (1): %d\n", isPalindrome(4, "abba"));
}