标签:color 使用 io strong 数据 ar cti div
输入一个字符串和一个非负整数N,要求将字符串循环左移N次。
输入格式:
输入在第1行中给出一个不超过100个字符长度的、以回车结束的非空字符串;第2行给出非负整数N。
输出格式:
在一行中输出循环左移N次后的字符串。
输入样例:Hello World! 2输出样例:
llo World!He
#include<stdio.h>
#include<string.h>
int main(){ char s[101],s2[101];
int n;
gets(s);
char *p=s;
scanf("%d",&n);
n= n % strlen(s);//左移长度判断
p=p+n;
strcpy(s2,p);//标志右边的复制给s2
//s2[n]=‘\0‘;
*p=‘\0‘;//s修改成标志左边的字符串
//strcpy(s,p);
strcat(s2,s);//连接操作完成左移;
puts(s2);
return 0;
}
#include<stdio.h>#include<string.h>#include<stdlib.h> int main(void){ const int length_of_array = 101; //定义数组长度 char sentence[length_of_array]; char *temporary = (char*)malloc(sizeof(char)*length_of_array); //临时内存空间 int shift; //偏移量 gets(sentence); scanf("%d",&shift); int length_of_sentence = strlen(sentence); //取得输入数据的长度 if ( shift > length_of_sentence){ //处理偏移量大于数据长度的情况 shift = shift % length_of_sentence; } char *located_address = sentence + shift; //该地址是“相对原始地址来说偏移shift后的地址 ” strcpy(temporary,located_address); //将偏移后地址的全部内容复制到临时容器中 *located_address = ‘\0‘; //将偏移后的地址对应的数据修改为‘\0‘供下面使用 strcat(temporary,sentence); //原始地址的数据内容直接追加到临时空间 printf("%s",temporary); free(temporary); return 0;}标签:color 使用 io strong 数据 ar cti div
原文地址:http://www.cnblogs.com/emochuanshuo/p/3918649.html