468,513 Members | 1,763 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,513 developers. It's quick & easy.

Creating a Concordance of Word Count for a Text File

3
i am a beginer in c programming and i am trying to Create a Concordance of Word Count for a Text File but my code is not working.pls can anyone helpme out.here is my code:
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. struct word {
  6. struct word *left; /* tree to the left */
  7. struct word *right; /* tree to the right */
  8. char *WORD;
  9. int count;
  10. }word;
  11. static struct word *root = NULL;
  12. void memory_error(void)
  13. {
  14. fprintf(stderr, "not enough memory\n");
  15. exit(8);
  16. }
  17. char *fp(char *fp)
  18. {
  19. char *p; 
  20. p = (char*)malloc((unsigned) (strlen(fp) + 1));
  21. if (p == NULL)
  22. memory_error();
  23. strcpy(p, fp);
  24. int compword(const void *WORD1, const void *WORD2);
  25. return (p);
  26. }
  27. void enter(struct word **word, char *WORD)
  28. {
  29. int result; 
  30. char *fp(char *); 
  31. if ((*word) == NULL) {
  32. (*word) = ((word*)malloc(sizeof(struct word)));
  33. if ((*word) == NULL)
  34. {printf("not enough mem");
  35. exit(1);
  36. }
  37. }
  38. (*word)->left = NULL;
  39. (*word)->right = NULL;
  40. (*word)->WORD = fp(WORD);
  41. (*word)->count=1;
  42. return;
  43. }
  44. result = strcmp((*WORD)->word,WORD);
  45. if (result == 0);
  46. {
  47. (*word)->count+=1;
  48. return;
  49. }
  50. if (result < 0);
  51. {{
  52. enter(&((*word)->right),WORD) ;
  53. else
  54. enter(&((*word)->left),WORD);
  55. }
  56. void scan(char *name)
  57. {
  58. char WORD[100];
  59. int index; 
  60. int ch; 
  61. FILE *f; 
  62. f = fopen(name, "r");
  63. if (f == NULL) {
  64. fprintf(stderr, "Unable to open %s\n", name);
  65. exit(8);
  66. }
  67. while (1) {
  68. ch = fgetc(f);
  69. if (isalpha(ch) || (ch == EOF))
  70. break;
  71. }
  72. if (ch == EOF)
  73. break;
  74. WORD[0] = ch;
  75. for (index = 1; index < sizeof(WORD); ++index) {
  76. ch = fgetc(f);
  77. if (!isalpha(ch))
  78. break;
  79. WORD[index] = ch;
  80. }
  81. WORD[index] = '\0';
  82. enter(&root, WORD);
  83. }
  84. fclose(f);
  85. }
  86. void print_tree(struct word *top)
  87. {
  88. if (top == NULL)
  89. return; 
  90. print_tree(top->left);
  91. printf("%s\n", top->WORD);
  92. print_tree(top->right);
  93. }
  94. int main(int argc, char *argv[])
  95. {
  96. if (argc != 2) {
  97. fprintf(stderr, "Wrong number of parameters\n");
  98. fprintf(stderr, " on the command line\n");
  99. fprintf(stderr, "Usage is:\n");
  100. fprintf(stderr, " words 'file'\n");
  101. exit(8);
  102. }
  103. scanf(argv[1]);
  104. print_tree(root);
  105. return (0);
  106. }
  107.  
would be really grateul or youy help.
Dec 17 '07 #1
2 4337
sicarie
4,677 Expert Mod 4TB
How is this not working? Is it not compiling? What are the errors? Is there a logical error? What is it? What is it supposed to look like?
Dec 17 '07 #2
Asian
1
Actually the program should be able to accept text input or a statement from user and create an index of the words used in the text and the corresponding count for each word.

e.g.
Text: China is the most populated country in the world and poverty is great but nevertheless, they are catching up.

Index:

China - 1
is - 2
the - 2
most - 1
populated - 1
.
.
.
(and so on)

I also have difficulty to do this program.
Dec 19 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by jester.dev | last post: by
2 posts views Thread by elziko | last post: by
3 posts views Thread by waynejr25 | last post: by
1 post views Thread by beanie | last post: by
6 posts views Thread by boyindie86 | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.