473,372 Members | 1,343 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,372 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 4917
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.