copy1.c 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /* copy1.c -- strcpy() demo */
  2. #include <stdio.h>
  3. #include <string.h> // declares strcpy()
  4. #define SIZE 40
  5. #define LIM 5
  6. char * s_gets(char * st, int n);
  7. int main(void)
  8. {
  9. char qwords[LIM][SIZE];
  10. char temp[SIZE];
  11. int i = 0;
  12. printf("Enter %d words beginning with q:\n", LIM);
  13. while (i < LIM && s_gets(temp, SIZE))
  14. {
  15. if (temp[0] != 'q')
  16. printf("%s doesn't begin with q!\n", temp);
  17. else
  18. {
  19. strcpy(qwords[i], temp);
  20. i++;
  21. }
  22. }
  23. puts("Here are the words accepted:");
  24. for (i = 0; i < LIM; i++)
  25. puts(qwords[i]);
  26. return 0;
  27. }
  28. char * s_gets(char * st, int n)
  29. {
  30. char * ret_val;
  31. int i = 0;
  32. ret_val = fgets(st, n, stdin);
  33. if (ret_val)
  34. {
  35. while (st[i] != '\n' && st[i] != '\0')
  36. i++;
  37. if (st[i] == '\n')
  38. st[i] = '\0';
  39. else // must have words[i] == '\0'
  40. while (getchar() != '\n')
  41. continue;
  42. }
  43. return ret_val;
  44. }