/04/rostring/rostring.c

https://github.com/fwuensche/42-exam-miner · C · 60 lines · 38 code · 6 blank · 16 comment · 18 complexity · 7a3d43286bb764106a8bbe26aa4a0ce1 MD5 · raw file

  1. /* ************************************************************************** */
  2. /* */
  3. /* ::: :::::::: */
  4. /* rostring.c :+: :+: :+: */
  5. /* +:+ +:+ +:+ */
  6. /* By: fwuensche <fwuensche@student.42.fr> +#+ +:+ +#+ */
  7. /* +#+#+#+#+#+ +#+ */
  8. /* Created: 2016/12/02 17:47:50 by angavrel #+# #+# */
  9. /* Updated: 2019/03/05 08:51:49 by fwuensche ### ########.fr */
  10. /* */
  11. /* ************************************************************************** */
  12. #include <unistd.h>
  13. int ft_isblank(char c)
  14. {
  15. return (c == ' ' || c == '\t');
  16. }
  17. void rostring(char *s)
  18. {
  19. int i = 0;
  20. int first_word_length = 0;
  21. while (s[i])
  22. {
  23. // skip beggining whitespaces
  24. while (ft_isblank(s[i]))
  25. i++;
  26. if (s[i] && !ft_isblank(s[i]))
  27. {
  28. // if it's the beggining
  29. if (first_word_length == 0)
  30. // keep the length of first word for later
  31. while (s[i] && !ft_isblank(s[i++]))
  32. first_word_length++;
  33. else
  34. {
  35. // for other words, just write it to stdout (+ one whitespace)
  36. while (s[i] && !ft_isblank(s[i]) && write(1, &s[i++], 1));
  37. write(1, " ", 1);
  38. }
  39. }
  40. }
  41. // write first word
  42. i = 0;
  43. while (ft_isblank(s[i]))
  44. i++;
  45. while (first_word_length--)
  46. write(1, &s[i++], 1);
  47. }
  48. int main(int ac, char **av)
  49. {
  50. if (ac > 1 && *av[1])
  51. rostring(av[1]);
  52. write(1, "\n", 1);
  53. return (0);
  54. }