2023년 11월 28일 화요일

C++(Algorithm) - 이상한 문자 만들기




짝수자리는 대문자로, 홀수자리는 소문자로 바꾸면 된다.
 

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 ++ 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 으로 돌아가 남은 문자만큼
대소문자 변환을 해준다.


결과




댓글 없음:

댓글 쓰기

c++ thread.h

 c++에서 쓰레드 돌릴려면 thread.h 헤더를 쓰면 되는데 이 친구는 쓰레드가 아직 실행 중인지, 아니면 강제 종료하거나 하는 함수가 없어서 조금 아쉬운 애다. std::thread 는 로컬 변수로 선언하든 new 동적 할당을 하든 start 함...