1. 程式人生 > >PAT 10-4. 字串迴圈10-4. 字串迴圈左移(20)

PAT 10-4. 字串迴圈10-4. 字串迴圈左移(20)

##10-4. 字串迴圈10-4. 字串迴圈左移(20)
—-

輸入一個字串和一個非負整數N,要求將字串迴圈左移N次。

輸入格式:

輸入在第1行中給出一個不超過100個字元長度的、以回車結束的非空字串;第2行給出非負整數N。

輸出格式:

在一行中輸出迴圈左移N次後的字串。

輸入樣例:
Hello World!

2

輸出樣例:

llo World!He

—-

* ac版:

—-

/*10-4*/
#include<stdio.h>
int main(){
char *str = malloc(300*sizeof(char));
int n;
gets(str);
n=2;
scanf("%d",&n);
//輸入模組 
int length = 0;
length = strlen(str);
if(n==length){
    return 0;
}
else if(n>length)
{
    n=n%length;
}
char *start= malloc(300*sizeof(char));//防止越界 
start =str;
/**************************/
char *temp = malloc(101*sizeof(char)); 
int i=0;
for(i=0; i<n;i++){
*temp = *start;
temp++;
start++;
}
*temp='\0';
temp-=n;
printf("%s",(start));
printf("%s",(temp));
 
return 0;
}