判断字符串是否为回文
2005-05-13 19:49
453 查看
//判断字符串是否为回文如:‘123321’两边对称的字符串#include <stdio.h>#include <string.h>
#define DEBUG//调试用
/***************/
#ifndef FUNCTIONS
#define FUNCTIONS
///////////////////////////////////////////////////
char * BeginToEnd (const char * str , char * temp)
// 作用:temp放入 str的反序字符串
{
int i= strlen(str) ;
int j= -1;
int itemp = i;
#ifdef DEBUG
printf("strlen(str):%d",i);
#endif
while( --i > -1 && ++j <= itemp )
{
temp[j] = str[i] ;
#ifdef DEBUG
printf("/t%c:%c",str[i],temp[j]);
#endif
}
temp[++j] = str[strlen(str)];
#ifdef DEBUG
printf("/n %s : %d.%d.. :%c/n",temp, j,i , str[strlen(str)] );
#endif
return temp;
}
/////////////////////////////////////////
void FreeStr(char * ptr)
{
free(ptr);
return;
}
///////////////////////////////////////////
int CheckHuiWen(const char * const str)
//判断str是否为 ‘回文’
{
int i;
char* temp= (char *)
malloc ( sizeof(char)*( strlen(str)+1 ) ) ;
/*strncpy(temp , str, strlen(str)+1); */
BeginToEnd(str,temp);
i= strcmp(str , temp );
FreeStr(temp);
return i;
}
#endif
/****************************/
/************test******************/
void main(void)
{
char *testStr;
printf("input string :");
scanf("%s",testStr);
switch ( CheckHuiWen( testStr ) )
{
case 0: {
printf("%s HuiWen ...",testStr );
break;
}
default:{
printf("%s ! isn't HuiWen ... ",testStr );
}
}
return;
}
#define DEBUG//调试用
/***************/
#ifndef FUNCTIONS
#define FUNCTIONS
///////////////////////////////////////////////////
char * BeginToEnd (const char * str , char * temp)
// 作用:temp放入 str的反序字符串
{
int i= strlen(str) ;
int j= -1;
int itemp = i;
#ifdef DEBUG
printf("strlen(str):%d",i);
#endif
while( --i > -1 && ++j <= itemp )
{
temp[j] = str[i] ;
#ifdef DEBUG
printf("/t%c:%c",str[i],temp[j]);
#endif
}
temp[++j] = str[strlen(str)];
#ifdef DEBUG
printf("/n %s : %d.%d.. :%c/n",temp, j,i , str[strlen(str)] );
#endif
return temp;
}
/////////////////////////////////////////
void FreeStr(char * ptr)
{
free(ptr);
return;
}
///////////////////////////////////////////
int CheckHuiWen(const char * const str)
//判断str是否为 ‘回文’
{
int i;
char* temp= (char *)
malloc ( sizeof(char)*( strlen(str)+1 ) ) ;
/*strncpy(temp , str, strlen(str)+1); */
BeginToEnd(str,temp);
i= strcmp(str , temp );
FreeStr(temp);
return i;
}
#endif
/****************************/
/************test******************/
void main(void)
{
char *testStr;
printf("input string :");
scanf("%s",testStr);
switch ( CheckHuiWen( testStr ) )
{
case 0: {
printf("%s HuiWen ...",testStr );
break;
}
default:{
printf("%s ! isn't HuiWen ... ",testStr );
}
}
return;
}
相关文章推荐
- 判断字符串是否是回文
- 判断字符串是否回文
- 判断一个字符串是否回文?
- 【C语言】判断一个字符串是否为回文字符串。
- C语言OJ项目参考(2802)判断字符串是否为回文
- AC日记——判断字符串是否为回文 openjudge 1.7 33
- 判断字符串是否为回文
- YTU 2802: 判断字符串是否为回文
- 判断一个字符串、整数是否为回文字符串或回文数
- Problem C: 判断字符串是否为回文
- 判断一个字符串是否为回文(递归)
- js判断一个字符串是否是回文字符串
- 递归判断一个字符串是否回文
- 编写函数,判断一个字符串是否是回文
- HDU2163_判断字符串是否为回文_速度较慢
- OJ Problem F: 判断字符串是否为回文
- 判断一个字符串是否为回文的递归算法
- 队列-----判断一个字符串是否是回文
- valid-palindrome——判断带符号数字字母的字符串是否为回文
- 第十五周oj训练——判断字符串是否为回文(2802)