1
0

names2.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /* names2.c -- passes and returns structures */
  2. #include <stdio.h>
  3. #include <string.h>
  4. #define NLEN 30
  5. struct namect {
  6. char fname[NLEN];
  7. char lname[NLEN];
  8. int letters;
  9. };
  10. struct namect getinfo(void);
  11. struct namect makeinfo(struct namect);
  12. void showinfo(struct namect);
  13. char * s_gets(char * st, int n);
  14. int main(void)
  15. {
  16. struct namect person;
  17. person = getinfo();
  18. person = makeinfo(person);
  19. showinfo(person);
  20. return 0;
  21. }
  22. struct namect getinfo(void)
  23. {
  24. struct namect temp;
  25. printf("Please enter your first name.\n");
  26. s_gets(temp.fname, NLEN);
  27. printf("Please enter your last name.\n");
  28. s_gets(temp.lname, NLEN);
  29. return temp;
  30. }
  31. struct namect makeinfo(struct namect info)
  32. {
  33. info.letters = strlen(info.fname) + strlen(info.lname);
  34. return info;
  35. }
  36. void showinfo(struct namect info)
  37. {
  38. printf("%s %s, your name contains %d letters.\n",
  39. info.fname, info.lname, info.letters);
  40. }
  41. char * s_gets(char * st, int n)
  42. {
  43. char * ret_val;
  44. char * find;
  45. ret_val = fgets(st, n, stdin);
  46. if (ret_val)
  47. {
  48. find = strchr(st, '\n'); // look for newline
  49. if (find) // if the address is not NULL,
  50. *find = '\0'; // place a null character there
  51. else
  52. while (getchar() != '\n')
  53. continue; // dispose of rest of line
  54. }
  55. return ret_val;
  56. }