Created
November 1, 2024 15:03
-
-
Save AdrianSkar/41053a0bd473983353f31fe92b757225 to your computer and use it in GitHub Desktop.
Testing exam exercise, ./a.out " " returns USER=username
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <unistd.h> | |
#include <stdint.h> | |
#include <stdarg.h> | |
/* | |
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$ | |
$> | |
*/ | |
int good_char(int c) | |
{ | |
if (c == ' ' || c == '\t' || c == '\0') | |
return 0; | |
return (1); | |
} | |
int main(int argc, char **argv) | |
{ | |
if (argc != 2 || !argv[1] || !argv[1][0]) | |
{ | |
write(1, "\n", 1); | |
return 0; | |
} | |
int i = 0; | |
while (!good_char(argv[1][i])) // skip first | |
i++; | |
while (argv[1][i]) | |
{ | |
if (!good_char(argv[1][i])) | |
{ | |
write(1, "\n", 1); | |
return 0; | |
} | |
write(1, &argv[1][i], 1); | |
i++; | |
} | |
return (0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment