请用带么写一个算法测试回环字符串函数 - 高飞网
5人看过

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

2014-04-20 01:19:09

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

}

还没有评论!
54.198.28.114