标签:c语言
------------------------------------------------------------------------------------------
例如:字符串:AABCD 左旋一个字符为:ABCDA
左旋两个字符为:BCDAA
------------------------------------------------------------------------------------------
C语言代码:
# include <stdio.h> # include <stdlib.h> # include <string.h> # define MAX 20 void Contra_solem(char *str, int move) { char *pstart = NULL; char *pend = NULL; char *src = NULL; int i = 0; pstart = str; pend = (str + strlen(str)); src = str; for (i = 0; i < move; i++) { *pend++ = *pstart++; } while (pstart <= pend) { *src = *pstart; pstart++; src++; } } int main() { char str[MAX] = "abcdefgh"; int move = 0; printf("源字符串为:%s\n",str); printf("输入想要左旋字符的个数:"); scanf("%d", &move); while (move > strlen(str)) { printf("@@@输入太大,请重新输入!@@@\n"); scanf("%d",&move); } Contra_solem(str,move); printf("左旋%d个字符后的字符串为:%s\n",move,str); system("pause"); return 0; }
------------------------------------------------------------------------------------------
干活小知识:C语言中无BOOL类型,将BOOL类型和零值比较时用if(bool)或者if(!bool);
------------------------------------------------------------------------------------------
本文出自 “无名小卒” 博客,请务必保留此出处http://814193594.blog.51cto.com/10729329/1708923
标签:c语言
原文地址:http://814193594.blog.51cto.com/10729329/1708923