回文小例子

#include <string.h>
#include <stdio.h>

#define MAX 100

char* strrev(char* szT)
{
	int k,j;
	if ( !szT )                 // 处理传入的空串.
	return "";
	int i = strlen(szT);
	int t = !(i%2)? 1 : 0;      // 检查串长度.
	for(j = i-1 , k = 0 ; j > (i/2 -t) ; j-- )
	{
		char ch  = szT[j];
		szT[j]   = szT[k];
		szT[k++] = ch;
	}
	return szT;
}

int IsHuiwen(char * text)
{
	enum {NOT_HUIWEN,HUIWEN};
	int i;
	int j = 0;
	char buf[MAX];
	for(i = 0; i < strlen(text); i++)
	{
		if( (*(text+i) >= 'a' && *(text+i) <= 'z') || (*(text+i) >= 'A' && *(text+i) <= 'Z') )
			buf[j++] = text[i];
	}
	if(strcasecmp(buf,strrev(buf)) == 0)
		return HUIWEN;
	else
		return NOT_HUIWEN;
}

int main(void)
{
	char * text = "was it a cat I saw";
	char array[10] = "hello";
	printf("%s\n",strrev(array));
	if(IsHuiwen(text))
		printf("the text is Huiwen !\n");
	else
		printf("the text is not Huiwen !\n");
	return 0;
}
注意:strrev()在linux中没有此函数

你可能感兴趣的:(回文小例子)