1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | #include <stdio.h> #include <stdbool.h> #include <stdlib.h> #include <string.h> #include <ctype.h> // 파라미터로 주어지는 문자열은 const로 주어집니다. 변경하려면 문자열을 복사해서 사용하세요. char* solution(const char* s) { // return 값은 malloc 등 동적 할당을 사용해주세요. 할당 길이는 상황에 맞게 변경해주세요. char* answer = (char*)malloc(strlen(s) + 1); if (answer != NULL)//exception { strcpy_s(answer, strlen(s) + 1, s); } else { return 0; } for (int x = 0; x < strlen(answer); x++) { if (x != 0) { if (*(answer + x) != ' ') continue; x++; } for (int y = 0; y < strlen(answer) - x; y++) { if (*(answer +x + y) == ' ')break; if (y % 2 == 0) { *(answer + x + y) = toupper(*(answer + x + y)); } else { *(answer + x + y) = tolower(*(answer + x + y)); } } } return answer; } void main() { char* rtn = solution("try hello world"); printf("%s", rtn); free(rtn); } | cs |
malloc을 쓰라고 해둬서 char*을 쓰게 됐음
공백있는 자리는 건너뛰고 다시 0 으로 돌아가 남은 문자만큼
대소문자 변환을 해준다.
결과
댓글 없음:
댓글 쓰기