永发信息网

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

答案:1  悬赏:10  手机版
解决时间 2021-04-07 02:36
  • 提问者网友:留有余香
  • 2021-04-06 05:21
题目二. 编程题:编写程序实现字符串循环左移,左移的位数由程序运行时从键盘输入。
最佳答案
  • 五星知识达人网友:杯酒困英雄
  • 2021-04-06 05:31
#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("
");
}

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] = "abcdefghi";

show_res(str, 10);
rotate_count(str, 2);
show_res(str, 10);
return 0;
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯