공부/Baekjoon

Baekjoon(9093) 단어 뒤짚기 in C

완이버스 2024. 1. 17. 00:02
#include <stdio.h>
#include <string.h>
#define infinite 1000

char input[1000];

int main(){
    int N;
    char stack[21];
    char input[1000];
    int cnt = 0;
    scanf("%d",&N);
    getchar();
    for(int i = 0; i < N; i++){
        fgets(input,infinite,stdin);
        for(int j = 0; j < strlen(input) - 1; j++){
            if(input[j] != ' '){
                stack[cnt++] = input[j];
            }
            if(input[j] == ' ' || j == strlen(input) - 2){
                for(int x = cnt - 1;x >= 0;x--){
                    printf("%c",stack[x]);
                }
                printf(" ");
                cnt = 0;
            }
        }
        printf("\n");
    }
}

 

+ 코드는 다음과 같이 작성하였는데 방법이 생각이 나질 않아 인터넷의 힘을 좀 빌렸다...

알고 나니까 왜 생각 못했지 라는 생각이 들고 그리고

terminal에서 testcase입력시에 중간에 newline이 껴있어서 뭐가 문제인지 봤더니

scanf()와 fgets() 사이에서 scanf()가 정수를 받고 \n가 buffer에 남아있었기 때문이였다

그래서 getchar()를 통해 입력 버퍼를 사라지게 해준다는 점도 배움...

'공부 > Baekjoon' 카테고리의 다른 글

Baekjoon(9012) 괄호 in C  (0) 2024.01.18
Baekjoon(10828) C Language  (0) 2024.01.16