1
0

format.c 883 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /* format.c -- format a string */
  2. #include <stdio.h>
  3. #define MAX 20
  4. char * s_gets(char * st, int n);
  5. int main(void)
  6. {
  7. char first[MAX];
  8. char last[MAX];
  9. char formal[2 * MAX + 10];
  10. double prize;
  11. puts("Enter your first name:");
  12. s_gets(first, MAX);
  13. puts("Enter your last name:");
  14. s_gets(last, MAX);
  15. puts("Enter your prize money:");
  16. scanf("%lf", &prize);
  17. sprintf(formal, "%s, %-19s: $%6.2f\n", last, first, prize);
  18. puts(formal);
  19. return 0;
  20. }
  21. char * s_gets(char * st, int n)
  22. {
  23. char * ret_val;
  24. int i = 0;
  25. ret_val = fgets(st, n, stdin);
  26. if (ret_val)
  27. {
  28. while (st[i] != '\n' && st[i] != '\0')
  29. i++;
  30. if (st[i] == '\n')
  31. st[i] = '\0';
  32. else // must have words[i] == '\0'
  33. while (getchar() != '\n')
  34. continue;
  35. }
  36. return ret_val;
  37. }