aff_a.c 933 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /* Assignment name : aff_a
  2. Expected files : aff_a.c
  3. Allowed functions: write
  4. --------------------------------------------------------------------------------
  5. Write a program that takes a string, and displays the first 'a' character it
  6. encounters in it, followed by a newline. If there are no 'a' characters in the
  7. string, the program just writes a newline. If the number of parameters is not
  8. 1, the program displays 'a' followed by a newline.
  9. Example:
  10. $> ./aff_a "abc" | cat -e
  11. a$
  12. $> ./aff_a "dubO a POIL" | cat -e
  13. a$
  14. $> ./aff_a "zz sent le poney" | cat -e
  15. $
  16. $> ./aff_a | cat -e
  17. a$ */
  18. #include <unistd.h>
  19. int main(int argc, char **argv)
  20. {
  21. (void) *argv;
  22. char a;
  23. int i;
  24. char n;
  25. a = 'a';
  26. i = 0;
  27. n = '\n';
  28. if (argc != 2)
  29. write(1, &a, 1);
  30. else
  31. while (argv[1][i] != '\0')
  32. {
  33. if (argv[1][i] == 'a')
  34. {
  35. write(1, &a, 1);
  36. write(1, &n, 1);
  37. return (0);
  38. }
  39. else
  40. i++;
  41. }
  42. write(1, &n, 1);
  43. }