C语言实现左旋字符串
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 5 void left_rotate(char *str, int k) 6 { 7 int len=strlen(str); 8 k %= len;//减少重复次数 9 10 char temp = 0; 11 char *cur = str; 12 while (k--) 13 { 14 15 //左旋一次 16 temp = *str; 17 cur = str; 18 while (*(cur + 1) != '\0') 19 { 20 //每个字符向前推一个 21 *cur = *(cur + 1); 22 cur++; 23 } 24 25 *cur = temp; 26 } 27 } 28 29 30 31 int main() 32 { 33 char str[100] = "abcdefg"; 34 left_rotate(str, 2); 35 printf("%s\n", str); 36 37 system("pause"); 38 return 0; 39 40 }