注册 登录
编程论坛 C语言论坛

编写一个函数 loopMove 实现字符串的循环右移功能,怎么感觉只能一位找了很久都没有找到错误,大神求解

dakou 发布于 2017-08-20 16:19, 2809 次点击
#include<stdio.h>
#include<string.h>
void loopmove(char* sta,int steps){
    int a=0,b=0;
    char s;
    for(a;a<steps;++a){
        for(b;b<strlen(sta)-1;++b){
           s   =   sta[strlen(sta)-1];
           sta[strlen(sta)-1]=sta[b];
           sta[b]=s;
        }
    }
    //return sta;
}
int main(){
    char str[]="hello";
    //char str1[]="abcdef";
    /*char* yy=*/loopmove(str,3);
    printf("%s\n",str);
}
5 回复
#2
wp2319572017-08-21 09:07
for(b=0;b<strlen(sta)-1;++b)   //内循环变量计步器没有设置初始化
#3
a10170750432017-08-21 09:35
程序代码:
#include<stdio.h>
#include<string.h>
void loopmove(char* sta,int steps){
    int a,b;
    char s;
    for(a=0;a<steps;++a){
        for(b=0;b<strlen(sta)-1;++b){
           s   =   sta[strlen(sta)-1];
           sta[strlen(sta)-1]=sta[b];
           sta[b]=s;
        }
    }
    //return sta;
}
int main(){
    char str[]="hello";
    //char str1[]="abcdef";
    /*char* yy=*/loopmove(str,3);
    printf("%s\n",str);
}
#4
marlow2017-08-21 22:19
回复 楼主 dakou
正确的循环右移算法如下:
程序代码:
void loopmove(char* str, int steps){
    int i = 0, j;
    char s;
    steps = steps % strlen(str);
    while(i++ < steps){
        s = *str;
        for(j = 0; *(str+j+1) != '\0'; j++)
            *(str + j) = *(str+j+1);
        *(str+j) = s;
    }
}
#5
marlow2017-08-21 22:23
另外,有谁知道层次更高一些的C语言论坛?分享一下,多谢了!
#6
lmlm10012017-08-22 21:51
to 5楼:
        github社区、Linux社区、unix社区
1