您的位置:首页 > 其它

判断字符串是否为回文

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;
 
             }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  string input c