设为首页 加入收藏

TOP

请用带么写一个算法测试回环字符串函数
2014-10-19 00:08:30 来源: 作者: 【 】 浏览:40
Tags:一个 算法 测试 字符串 函数

#include


#include


#define false 0


#define true 1




/*判断一个字符串是不是回环字符串函数*/


int isok(char *str)


{


int len = strlen(str);


int i = 0, j = len – 1;


while (i < j)


{


if(str[i] != str[j])


return false;


i++; j–;


}


return true;


}


/*如果不是回环字符串,在字符串的头部添加字符串,让这个字符串成为回环字符串*/


int addhead(char *str)


{


int len = strlen(str);


printf(“str num = %d\n”,len);


char tmp[2*len];


int pos = 0, j = len – 1;


while(pos < len -1)


{


tmp[pos++] = str[j--];


printf(“xun huan\n”);


}


tmp[pos] = ‘\0′;


strncat(tmp,str,len);


printf(“tmp string is %s\n”,tmp);


if(isok(tmp))


printf(“successful \n”);


}


/*如果不是回环字符串,在函数的尾部添加字符串,让这个字符串成为回环字符串*/


int addtail(char *str)


{


int len = strlen(str);


int i=0, k=0,j;


char tmp[2*len];


while (i < len )


{


tmp[i++] = str[k++];


}


j = i;


i = 0;


k = len – 1;


while(i < len -1)


{


tmp[j++] = str[k--];


}


tmp[j] = ‘\0′;


printf(“tmp = %s\n”, tmp);


if(isok(tmp))


printf(“successful \n”);


}


/*测试回环函数的主函数*/


int main(int argc, char *argv[])


{


char *str = “abc”;


int num = isok(str);


if(num == 1)


{


printf(“this is string\n”);


}


else {


printf(“this is the not string\n”);


num = addhead(str);


num = addtail(str);


}


return 0;


}


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇请用C/C++实现反转单链表算法 下一篇UC 优视科技字符串算法面试题

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: