반응형

문제). 
문자열로 된 문장을 띄어쓰기 단위(단어 단위)로 잘라서 단어를 역순으로 출력하시오.

 

 

실행 예1).

 

입력)

"This is a pen."

 

결과).

pen. a is This

 

 

 

 

 

 

 

 

 

 

 


답은 아래에... ↓

 

 

 

 

 

 

 


 

 

 

 

 

스스로 풀어보시고... ↓

 

 

 

 

 

 


 

 

 

 

 

 

아래 답과 비교해보세요. ↓

 

 

 

 

 

 


프로그램 소스

#include <stdio.h>
#include <string.h>

int main()
{
    char s[]="Hi my name is";
    char *words[1024];
    int  word_count = 0;
    
    words[word_count] = strtok(s, " ");
    
    while(words[word_count]) {
        word_count++;
        words[word_count] = strtok(NULL, " ");
    }
    
    for(int idx = word_count - 1; idx >=0; idx--) {
        printf("%s ", words[idx]);
    }

    return 0;
}

 

 

 

 

 

반응형
블로그 이미지

자연&사람

행복한 개발자 programmer since 1995.

,