user-0 提问: 编写一函数strlshif(char *s,int n),

回复:
user: user-1
content: char *strlshif(char *s,int n) {     char *t;     int L;      L=strlen(s);     if (n<=0 || n<L) return s;     t=(char *)malloc(L+1);     sprintf(t,"%.*s%.*s",L-n,s+n,n,s);     strcpy(s,t);     free(t);     return s; }

user: user-2
content: 补个翻手的实现，比较有意思 void swap(char &a, char& b) { 	char c = a; 	a = b; 	b = c; }  void reverse(char* s,int n) { 	for(int i=0;i<n/2;i++){ 		swap(s[i],s[n-1-i]);	 	} }  char *strshift(char* s, int n) { 	int len = strlen(s); 	reverse(s,n); 	reverse(s+n,len-n); 	reverse(s,len); 	return s; }

user: user-2
content: 记得编程之美中有个翻手算法， 3次翻手之后可以满足你的需求 ，而且是inplace的， 只需要实现reverse就可以了

user: user-1
content: 提醒：充分利用sprintf函数可以干很多事！比如 char ansi_str[10]="你好"; wchar_t unicode_str[5]; swprintf(unicode_str,L"%S",ansi_str); sprintf(ansi_str,"%S",unicode_str);  %S String When used with printf functions, specifies a wide-character string; when used with wprintf functions, specifies a single-byte–character string. Characters are printed up to the first null character or until the precision value is reached.

user: user-1
content: 引用 7 楼 zhangxiangDavaid 的回复:[quote=引用 4 楼 zhao4zhong1 的回复:] char *strlshif(char *s,int n) {     char *t;     int L;      L=strlen(s);     if (n<=0 || n<L) return s;     t=(char *)malloc(L+1);     sprintf(t,"%.*s%.*s",L-n,s+n,n,s);     strcpy(s,t);     free(t);     return s; }  有点小bug：   if (n<=0 || n<L) return s;  改为   if (n<=0 || n>=L) return s; [/quote] 谢谢斧正。 之前我也没实际调试，些微有点不负责任。

user: user-3
content: 引用 4 楼 zhao4zhong1 的回复:char *strlshif(char *s,int n) {     char *t;     int L;      L=strlen(s);     if (n<=0 || n<L) return s;     t=(char *)malloc(L+1);     sprintf(t,"%.*s%.*s",L-n,s+n,n,s);     strcpy(s,t);     free(t);     return s; }  有点小bug：   if (n<=0 || n<L) return s;  改为   if (n<=0 || n>=L) return s;

user: user-3
content: 不好意思，没有对参数，做安全检查

user: user-3
content: 这样可好：  char *strlshif(char *s, int n) { 	char *buf = malloc(n + 1); 	memset(buf, 0, n + 1); 	strncpy(buf, s, n); 	memmove(s, s + n, strlen(s) - n); 	strncpy(s + strlen(s) - n, buf, n); 	free(buf); 	return s; } int main(void) { 	char s[20]="zhangxiang"; 	printf("%s\n", s); 	printf("%s\n", strlshif(s, 3)); 	return 0; } //zhangxiang //ngxiangzha

user: user-4
content: #include <stdio.h> #include <string.h> #include <stdlib.h>  void strlshift(char *str, int n) { 	if (str == NULL) 		return;  	int length = strlen(str); 	n %= length; 	if (n == 0)         return; 	char *str_copy = malloc(length + 1); 	strcpy(str_copy, str);  	int i; 	for (i = 0; i < length; i++) 	{ 	    int k = (length - n + i) % length; 		str[k] = str_copy[i]; 	}      free(str_copy); }  int main(void) { 	char *str = malloc(20); 	char *const_str = "C language";     strcpy(str, const_str); 	strlshift(str, 1); 	printf("%s \n", str); 	strlshift(str, 10); 	printf("%s \n", str); 	strlshift(str, 9); 	printf("%s \n", str); 	free(str); }  我也是编程新手，共同学习哈

user: user-5
content: leetcode上一道类似的题 Rotate Array  Rotate an array of n elements to the right by k steps.  For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].  Note: Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.  Hint: Could you do it in-place with O(1) extra space? void rotate(int* nums, int n, int k) {     // 将数组元素依次循环向右平移k个单位     for (int i = 0, j = k % n, previous = nums[i], anchor = i, count = n; count > 0; count--, j = (i + k) % n)  {         int tmp = nums[j];         nums[j] = previous;         previous = tmp;         if ((i = j) == anchor) {             i = (i + 1) % n;             previous = nums[i];             anchor = i;         }     } } 你自己改改就可以用了。

user: user-6
content: 先见前n个字符copy到一个临时的地方 然后将后面的字符move到字符串开始的地方 最后将n个字符copy到后字符串的后n个地方