题目二. 编程题:编写程序实现字符串循环左移,左移的位数由程序运行时从键盘输入。

2024-11-04 09:44:09
推荐回答(1个)
回答1:

#include
#include
#include


void show_res(char *ch, int num){

int i;
for(i = 0; i < num; ++i)
if(ch[i] == 0)
printf("\\0 ");
else
printf("%c ", ch[i]);
printf("\n");
}

void rotate_shift_left_one(char *ch, int end){

char tmp = ch[0];
int i;
for(i = 1; i < end; ++i)
ch[i-1] = ch[i];
ch[end-1] = tmp;
}

int calc_end_pos(char *ch){

return strlen(ch);
}

void rotate_count(char *ch, int n){

int end_pos = calc_end_pos(ch);
int i;
for(i = 0; i < n; ++i)
rotate_shift_left_one(ch, end_pos);
}



int main(){
char str[100] = "abcdefg\0hi";

show_res(str, 10);
rotate_count(str, 2);
show_res(str, 10);
return 0;
}