Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #define MAX 1000
  5. #define SIZE 50
  6.  
  7. void read_line(char *n);
  8.  
  9. int main() {
  10. char string[MAX], words[MAX][SIZE], aux[SIZE];
  11. int i, j, k, n, count;
  12. char *token;
  13. const char s[4] = " .,!";
  14.  
  15. i = j = k = n = 0;
  16.  
  17. printf("Please enter the sentence that you would like to tokenize: ");
  18. read_line(string);
  19.  
  20. token = strtok(string, s);
  21. while( token != NULL )
  22. {
  23. strcpy(words[j], token);
  24. j++;
  25. token = strtok(NULL, s);
  26. }
  27.  
  28. n = j-1;
  29.  
  30. for (i = 0; i < n; i++) {
  31. strcpy(aux, words[i]);
  32. for (j = i + 1; j <= n; j++) {
  33. if (strcmp(words[i], words[j]) > 0) {
  34. strcpy(aux, words[j]);
  35. strcpy(words[j], words[i]);
  36. strcpy(words[i], aux);
  37. }
  38. }
  39. }
  40.  
  41. printf("Frequency:\n");
  42. i = 0;
  43.  
  44. while (i <= n) {
  45. count = 1;
  46. if (i != n) {
  47. for (j = i + 1; j <= n; j++) {
  48. if (strcmp(words[i], words[j]) == 0) {
  49. count++;
  50. }
  51. }
  52. }
  53. printf("%s\t%d\n", words[i], count);
  54. i = i + count;
  55. }
  56.  
  57. return 0;
  58. }
  59.  
  60. void read_line(char *n) {
  61. char ch;
  62. while ( (ch = getchar()) != '\n') {
  63. *n = ch;
  64. n++;
  65. }
  66. *n = '\0';
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement