=== 소 감 === *(pCh+lenstr-i-1) = temp[i]; 이 부분에서 자꾸 *(pCh+lenstr-i) = temp[i]; 이렇게 코딩해서, 컴파일은 되는데, 결과물이 안나와서 답답했었다. 널 문자를 생각 못했던 아영 ;; 그리고 char 함수를 처음 사용해서. 처음에 에러도 많이 났다. 그리고 파일 입출력도 익숙치 않다. 연습좀 해야겠다. 암튼 성공 ~! * ver2 를 만들려고 한다. 영어와 한글을 구분하는 것은 구했는데, 한글문자열을 반대로 출력하는 알고리즘이 떠오르지 않는다. 많이 고민중... +_+ * ver.2 완성 ! 원시적인 방법으로 했다. 리펙토링이 필요하다. 단점: 한글과 영어를 섞어서 사용 못한다는 점. 영어와 한글을 섞을 수 있는 ver.3 만드는 것이 문제. choiceNum을 영어, 한글, 문자로 세분화 하려고 했으니 일단은 저렇게 코딩. === 코 드 === ==== ver.1 ==== 영어문자열만 실행되는 프로그램 {{{~cpp /*파일에서 입력받은 문자열을 반대로 출력하는 프로그램*/ #include #include #include char strchange(char ch[50], int lenstr); void main() { char ch[50], *pCh; int lenstr; FILE *fp1, *fp2; pCh = ch; fp1 = fopen("source.txt", "r"); fgets(ch, 50, fp1); //파일에서 읽어옴 fclose(fp1); printf("Before string = %s \n", ch); lenstr = strlen(ch); *pCh = strchange(pCh, lenstr); fp2 = fopen("result.txt", "w"); printf("After string = %s \n", ch); fputs(ch, fp2); fclose(fp2); } char strchange(char *pCh, int lenstr) { int i; char temp[50]; for(i = 0; i <= lenstr; i++) { temp[i] = *(pCh+i); } for(i = 0; i <=lenstr; i++) { *(pCh+lenstr-i-1) = temp[i]; } return *pCh; } }}} ==== ver.2 ==== {{{~cpp /*파일에서 입력받은 문자열을 반대로 출력하는 프로그램 ver.2*/ #include #include #include char strchange(char ch[50], int lenstr, int choiceNum); void main() { char ch[50], *pCh; int lenstr, choiceNum, i = 0; FILE *fp1, *fp2; pCh = ch; fp1 = fopen("source.txt", "r"); fp2 = fopen("result.txt", "w"); fgets(ch, 50, fp1); //파일에서 읽어옴 lenstr = strlen(ch); if('A'<= ch[i] && ch[i] <='z') { choiceNum = 0; // 영어 }else { choiceNum = 1; // 한글?? } *pCh = strchange(pCh, lenstr, choiceNum); fputs(ch, fp2); fclose(fp1); fclose(fp2); } char strchange(char *pCh, int lenstr, int choiceNum) { int i; char temp[50]; switch(choiceNum) { case 0: for(i = 0; i <= lenstr; i++) { *(temp+i) = *(pCh+i); } for(i = 0; i <= lenstr; i++) { *(pCh+lenstr-i-1) = *(temp+i); } break; case 1: for(i = 0; i < lenstr; i++) { *(temp+i) = *(pCh+i); } for(i = 0; i < lenstr; i+=2) { *(pCh+lenstr-i-2) = *(temp + i); *(pCh+lenstr-i-1) = *(temp+i+1); } break; } return *pCh; } }}} ==== C++ 로 ==== {{{~cpp //cpp1.cpp #include #include #include "cpp.h" void main() { Mystring mystr; mystr.input(); mystr.str_reverse(); mystr.output(); } }}} {{{~cpp //cpp2.cpp #include #include "cpp.h" #include void Mystring::str_reverse() { str_len = strlen(ch); int i = 0; while(ch[i])// 간단히 영어만 된다. { str_temp[str_len-i-1] = ch[i]; ++i; } } void Mystring::input() { cin>>ch; } void Mystring::output() { int i = 0; while(str_temp[i] > 0) { cout<