1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- /* Assignment name : first_word
- Expected files : first_word.c
- Allowed functions: write
- --------------------------------------------------------------------------------
- Write a program that takes a string and displays its first word, followed by a
- newline.
- A word is a section of string delimited by spaces/tabs or by the start/end of
- the string.
- If the number of parameters is not 1, or if there are no words, simply display
- a newline.
- Examples:
- $> ./first_word "FOR PONY" | cat -e
- FOR$
- $> ./first_word "this ... is sparta, then again, maybe not" | cat -e
- this$
- $> ./first_word " " | cat -e
- $
- $> ./first_word "a" "b" | cat -e
- $
- $> ./first_word " lorem,ipsum " | cat -e
- lorem,ipsum$
- $> */
- #include <unistd.h>
- int main(int argc, char **argv)
- {
- int i;
- i = 0;
- if (argc != 2)
- {
- write(1, "\n", 1);
- }
- else
- {
- while (argv[1][i])
- {
- if (argv[1][i] >= 0 && argv[1][i] <= 32)
- i++;
- else
- {
- write(1, &argv[1][i], 1);
- i++;
- if (argv[1][i] >= 0 && argv[1][i] <= 32)
- {
- write (1, "\n", 1);
- return (0);
- }
- }
- }
- }
-
-
- return (0);
- }
|