字符串-05. 字符串循环左移

字符串-05. 字符串循环左移(20)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
白洪欢(浙江大学)

输入一个字符串和一个非负整数N,要求将字符串循环左移N次。

输入格式:

输入在第1行中给出一个不超过100个字符长度的、以回车结束的非空字符串;第2行给出非负整数N。

输出格式:

在一行中输出循环左移N次后的字符串。

输入样例:
Hello World!
2
输出样例:
llo World!He
 1 #include<stdio.h>
 2 #include<math.h>
 3 #include<stdlib.h>
 4 #include<string.h>
 5 int main()
 6 {
 7     char s[110], a[110], b[110];
 8     int n, i, j = 0, temp;
 9     gets(s);
10     scanf("%d", &n);
11     n = n % strlen(s);
12     for(i = n-1; i >= 0; i--)
13     {
14         a[j] = s[i];
15         j++;
16     }
17     a[j] = '\0';
18     j = 0;
19     for(i = strlen(s) - 1; i >= n; i--)
20     {
21         b[j] = s[i];
22         j++;
23     }
24     b[j] = '\0';
25     strcat(a, b);
26     for(i = strlen(s) - 1; i >= 0; i--)
27     {
28         printf("%c", a[i]);
29     }
30     return 0;
31 }

 

posted @ 2015-01-22 11:50  Yomman  阅读(859)  评论(0编辑  收藏  举报