str_cat.c 885 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /* str_cat.c -- joins two strings */
  2. #include <stdio.h>
  3. #include <string.h> /* declares the strcat() function */
  4. #define SIZE 80
  5. char * s_gets(char * st, int n);
  6. int main(void)
  7. {
  8. char flower[SIZE];
  9. char addon[] = "s smell like old shoes.";
  10. puts("What is your favorite flower?");
  11. if (s_gets(flower, SIZE))
  12. {
  13. strcat(flower, addon);
  14. puts(flower);
  15. puts(addon);
  16. }
  17. else
  18. puts("End of file encountered!");
  19. puts("bye");
  20. return 0;
  21. }
  22. char * s_gets(char * st, int n)
  23. {
  24. char * ret_val;
  25. int i = 0;
  26. ret_val = fgets(st, n, stdin);
  27. if (ret_val)
  28. {
  29. while (st[i] != '\n' && st[i] != '\0')
  30. i++;
  31. if (st[i] == '\n')
  32. st[i] = '\0';
  33. else // must have words[i] == '\0'
  34. while (getchar() != '\n')
  35. continue;
  36. }
  37. return ret_val;
  38. }