以前写过一个类似的实现,但是现在看了下,实现相对复杂,可读性也比较差,今天趁有时间就重新写一个:
形如字符串 “helloworld” 在顺时针旋转5个字节后就变成了“worldhello”:
-
char *
-
str_reverse(char *src, int k)
-
{
-
char *tmp, *rever;
-
-
if (src == NULL || k == 0) {
-
return src;
-
}
-
-
tmp = src;
-
while(*tmp != '\0') {
-
tmp++;
-
}
-
tmp--;
-
rever = tmp;
-
while (k) {
-
char c = *rever;
-
-
while(rever != src){
-
*rever = *(rever-1);
-
rever --;
-
}
-
*rever = c;
-
k--;
-
rever = tmp;
-
}
-
-
return src;
-
}
-
-
int main(void)
-
{
-
char str1[30]="helloworld";
-
str_reverse(str1, 5);
-
printf("reverse str1 = %s ,then %s\n", "helloworld", str1);
-
return 0;
-
}
阅读(1175) | 评论(0) | 转发(0) |