samir82show

capture last word function

Feb 15th, 2014
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #define MAX 30 // maximum word characters are 30
  4. void lastword_func(char *last_word); // func declaration
  5. int
  6. main()
  7. {
  8. char last_word[MAX];
  9. lastword_func(last_word);
  10. if (strcmp(last_word, "defbbasda") == 0)
  11. printf("right %s\n", last_word);
  12. else
  13. printf("wrong guess\n");
  14. return (0);
  15. }
  16. void
  17. lastword_func(char * last_word)
  18. {
  19. unsigned char i, state = 0;
  20. i = 0;
  21. int c;
  22. while ((c = getchar()) != EOF && i < MAX) {
  23. if (c == ' ' || c == '\t' || c == '\n')
  24. state = 1;
  25. else {
  26. if (state == 1) {
  27. i = 0;
  28. state = 0;
  29. }
  30. last_word[i++] = c;
  31. }
  32. }
  33. last_word[i] = '\0';
  34. }
Advertisement
Add Comment
Please, Sign In to add comment