C语言每日一练(第一天)
字符串左旋 例如:输入一个字符串abcd,输入左旋次数。 假如左旋了1次,为bcda。 假如左旋了2次,为cdab。 以下是自己写的代码(水平有限,仅供参考) #include #include //字符串左旋 #define MAXSIZE 20 void ZiFUStr_Left(char *strs, int left_c) { int i, k; int len; char tmp; len = strlen(strs); for(k = 1; k <= left_c; k++) { tmp = strs[0]; for(i = 1; i < len; i++) { strs[i-1] = strs[i]; } strs[len-1] = tmp; } } void ZiFUStr_Display(void) { char strs[MAXSIZE]; int left_c; printf("请输入一串字符个数小于等于%d的字符\n", MAXSIZE); scanf("%s", strs); printf("please input the lefts:\n"); scanf("%d", &left_c); printf("原字符串为:%s\n", strs); ZiFUStr_Left(strs, left_c); printf("左旋%d次后,字符串为:%s\n", left_c, strs); } int main(void) { ZiFUStr_Display(); return 0; }