입력받은 문자열을 원하는 방향, 횟수만큼 회전시켜준다.
'ABC'를 받으면 'ABCABCABC'의 문자열을 만든다.
그리고 가운데 문자열을 기준으로 +-이동하여 출력시켜준다.
직접 요소를 치환해가면서 이동하려면 머리아프다.
#include <stdio.h> // 문자열 이동시키기 // LANG,r,1 -> GLAN int strleng(char *str) { int cnt = 0;while (str[cnt] != '\0') cnt = cnt + 1; return cnt; } int main() { int i; int j; char str[50]; char d; int cnt; int leng; char str2[150]; printf("input string: "); scanf("%s", str); scanf("%c", &d); // stdin에 남아있는 개행문자 비우기 printf("input direction(L/R): "); scanf("%c", &d); printf("input move count: "); scanf("%d", &cnt); leng = strleng(str); for (i=0;i<=2;i++) { for (j=0;j<=leng;j++) str2[i*leng+j] = str[j]; } str2[i*j+1] = '\0'; if (cnt >= leng) cnt = cnt % leng; if (( d == 'r') || ( d == 'R')) cnt = - cnt; for(i=leng+cnt;i <= leng*2 -1 + cnt;i++) { printf("%c",str2[i]); } return 0; }
* 피시키드님에 의해서 게시물 이동되었습니다 (2024-04-26 18:49)