分享

请用代码写一个算法测试回环字符串函数

 看风景D人 2014-06-15

#include <stdio.h>

#include <string.h>

#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;

}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多