473,406 Members | 2,954 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 4923
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

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

Similar topics

5
by: jester.dev | last post by:
Hello, I'm learning Python from Python Bible, and having some problems with this code below. When I run it, I get nothing. It should open the file poem.txt (which exists in the current...
5
by: STeve | last post by:
Hey guys, I currently have a 100 page word document filled with various "articles". These articles are delimited by the Style of the text (IE. Heading 1 for the various titles) These articles...
2
by: elziko | last post by:
I'm trying to create a new table in word from a DataTable (sourceTable): objDoc.tables.Add(Range:=objDoc.Range, NumRows:=sourceTable.Rows.Count, NumColumns:=sourceTable.Columns.Count,...
5
eragon
by: eragon | last post by:
I wrote this function to create a new file when the user posts in my forums, and its not creating a new file, can you help me? this script is not copyrighted as the last one. function...
3
by: waynejr25 | last post by:
can anyone help me add a function that will count the occurance of each word in an input file. here's the code i have so far it counts the number of characters, words, and lines but i need the...
1
by: beanie | last post by:
i am a c programming beginner and i am trying to Create a concordance of Word Count for a text File in c programming but my code isn't working.please can u help me out.here is my code: #include...
6
by: boyindie86 | last post by:
Hi I have been fighting with this lump of code for the last week what I am trying to do is that I am passing words into passages of texts, and I want the system to go and find exact word matches...
0
by: alivip | last post by:
I write code to get most frequent words in the file I won't to implement bigram probability by modifying the code to do the following: How can I get every Token (word) and ...
5
by: alivip | last post by:
How can I get every Token (word) and PreviousToken(Previous word) From multube files and frequency of each two word my code is trying to get all single word and double word (every Token (word) and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.