实现对字符串进行循环右移

例如:输入abcdefgh,循环右移34位,输出ghabcdef

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include <stdio.h>  
  2. #include <string.h>  
  3.   
  4. #define MAX_SIZE 1024  
  5.   
  6. char *move(char *input,int n)  
  7. {  
  8.     if(input == NULL)  
  9.     {  
  10.         return NULL;  
  11.     }  
  12.   
  13.     int i;  
  14.     int len = strlen(input);  
  15.   
  16.     char *p = input;  
  17.     static char str[MAX_SIZE];  
  18.       
  19.     p = p + len - (n % len);  
  20.       
  21.     for(i = 0; i < (n % len); i++)  
  22.     {  
  23.         str[i] = *p;  
  24.         p++;  
  25.     }  
  26.     strcat(str,input);  
  27.   
  28.     str[len] = '\0';  
  29.   
  30.     return str;  
  31. }  
  32.   
  33. int main()  
  34. {  
  35.     char input[MAX_SIZE];  
  36.     char *result;  
  37.     int n;  
  38.   
  39.     printf("请输入任意字符串:");  
  40.     scanf("%s",input);  
  41.   
  42.     getchar();  
  43.   
  44.     printf("请输入循环右移位数:");  
  45.     scanf("%d",&n);  
  46.   
  47.     result = move(input,n);  
  48.   
  49.     printf("最终字符串:%s\n",result);  
  50.   
  51.     return 0;  
  52. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值