반응형

문제).
문자열로 된 문장을 띄어쓰기 단위(단어 단위)로 잘라서 단어를 역순으로 출력하시오.
실행 예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;
}
반응형
'C언어 문제 > 함수 활용' 카테고리의 다른 글
[strtok] 문자열에 포함된 단어의 수 출력하기 (0) | 2020.06.12 |
---|---|
[rand] 컴퓨터와 가위, 바위, 보 게임하기 (0) | 2019.11.19 |
[rand] 100 개의 데이터를 임의로 생성하여 정렬하시오. (0) | 2019.11.06 |
[strtok] 학점 계산기 (0) | 2019.11.04 |
[rand] 윷놀이 프로그램 (0) | 2019.11.03 |