공략법
- while 돌려서 끝까지
- 이후 i에서 1칸을 뺀다.
- 공백 이후의 문자 출력을 피하기 위함
- while 돌리기
- while로 공백인 부분 : end
- while로 공백 아닌 부분 : start
- start + 1 ~ end 까지 출력 (
str[start]는 공백) - 공백 1칸 출력
추가적으로 3번과 4번 사이에 if (start ≤ 0) break ; 를 넣는다.
이를 통해 마지막 문자 이후 아무것도 출력되지 않음을 보장!
코드
c
Assignment name : rev_wstr
Expected files : rev_wstr.c
Allowed functions: write, malloc, free
--------------------------------------------------------------------------------
Write a program that takes a string as a parameter, and prints its words in
reverse order.
A "word" is a part of the string bounded by spaces and/or tabs, or the
begin/end of the string.
If the number of parameters is different from 1, the program will display
'\n'.
In the parameters that are going to be tested, there won't be any "additional"
spaces (meaning that there won't be additionnal spaces at the beginning or at
the end of the string, and words will always be separated by exactly one space).
Examples:
$> ./rev_wstr "You hate people! But I love gatherings. Isn't it ironic?" | cat -e
ironic? it Isn't gatherings. love I But people! hate You$
$>./rev_wstr "abcdefghijklm"
abcdefghijklm
$> ./rev_wstr "Wingardium Leviosa" | cat -e
Leviosa Wingardium$
$> ./rev_wstr | cat -e
$
$>