C언어 문제/함수 활용
[strtok] 띄어쓰기 단위(단어 단위)로 잘라서 단어를 역순으로 출력
자연&사람
2020. 6. 12. 18:22
반응형
문제).
문자열로 된 문장을 띄어쓰기 단위(단어 단위)로 잘라서 단어를 역순으로 출력하시오.
실행 예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;
}
반응형