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

来源:互联网 发布:瑞赛网络 编辑:程序博客网 时间:2024/06/14 20:43

例如:输入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. }  
0 0