Assignment name : inter Expected files : inter.c Allowed functions: write -------------------------------------------------------------------------------- Write a program that takes two strings and displays, without doubles, the characters that appear in both strings, in the order they appear in the first one. The display will be followed by a \n. If the number of arguments is not 2, the program displays \n. Examples: $>./inter "padinton" "paqefwtdjetyiytjneytjoeyjnejeyj" | cat -e padinto$ $>./inter ddf6vewg64f gtwthgdwthdwfteewhrtag6h4ffdhsd | cat -e df6ewg4$ $>./inter "nothing" "This sentence hides nothing" | cat -e nothig$ $>./inter | cat -e $ -------------------------------------------------------------------------------- #include <unistd.h> int find_char_n(char c, char *str, int max) { int i = 0; while (str[i] != '\0') { if (max != -1 && i == max) return (0); if (c == str[i]) return (1); ++i; } return (0); } void inter(char *a, char *b) { int i = 0; while (a[i] != '\0') { if (find_char_n(a[i], b, -1) == 1 && find_char_n(a[i], a, i) == 0) write(1, a + i, 1); ++i; } } int main(int argc, char **argv) { if (argc == 3) inter(argv[1], argv[2]); write(1, "\n", 1); return (0); }
Standard