//객체지향 금 123 200911636 박정욱
//2012.5.18 2번 : Exercise 16-2 문자열 이동하기
#include<iostream>
using namespace std;
char* ShiftLeftString(char message[], int message_num); //문자열을 왼쪽으로 한칸 이동시키는 함수.
void main()
{
char message[] = "BINGO JJANG!!"; //message : BINGO ZZANG!!
cout << message << endl; //message 출력
char* copy = ShiftLeftString(message, 13); //copy는 "INGO JJANG!!B"가 된다.
cout << copy << endl; //copy 출력
char* copy2 = ShiftLeftString(copy, 13); //copy2는 "NGO JJANG!!BI"가 된다.
cout << copy2 << endl; //compy2 출력
delete copy; //할당받았던 메모리를 해제.
copy=NULL;
delete copy2;
copy2=NULL;
}
char* ShiftLeftString(char message[], int message_num) //문자열을 왼쪽으로 한칸 이동시키는 함수.
{
int i; //for문을 사용하기 위해 필요한 변수
char* shifted_message; //message_num 만큼의 메모리 공간을 저장할 변수
shifted_message= new char[message_num+1]; //메모리 할당
for(i=0; i < message_num-1; i++) //message의 0을 제외한 문자를 왼쪽으로 한칸씩 이동
{ shifted_message[i]=message[i+1]; }
shifted_message[i]=message[0]; //0번의 문자는 다른 문자열이 이동이 된후 이동시킨다.
shifted_message[message_num]=NULL; //마지막에는 NULL을 대입.
return shifted_message; //왼쪽으로 이동된 문자열을 리턴.
}