Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

word counting

Posted on 1998-11-22
14
Medium Priority
?
226 Views
Last Modified: 2010-04-15
In the following code, I am able to run a file thru it and strip out its symbols and numbers and count how many times this shows up in a file.  What I am unable to do is strip out each word and keep count on it and if possible to ignore anything in quotes.  I would appreciate a code that shows how this is done.  Thank You

#include<stdio.h>
#include<stlib.h>
#include<ctype.h>
#include<string.h>

int file_exists(char *filename);
main()
{
char ch, source[80];
int index;
long count[43];
FILE *fp;

fprint(stderr, "\Enter source file name: ");
gets(source);

if(!file_exists(source))
{
fprint(stderr, "\n%s does not exist.\n", source);
exit(1);
}
if((fp = fopen(source, "rb)) == NULL)
{
fprint(stderr, "\nError opening %s.\n", source);
exit(1);
}

for(index = 21; index < 63; index++)
count[index] = 0;

while(1)
{
ch = fgetc(fp);
if(feof(fp))
break;
if(ch > 21 && ch 63)
count[ch]++;
}

printf("\nChar\t\tCount\n");
for(index = 21; index < 63; index++)
printf("[%c]\t%d\n", index, count[index]);

fclose(fp);
return(0);
}
int file_exists(char *filename)
{
FILE *fp;
if ((fp = fopen(filename, "r")) == NULL)
return 0;
else
{
fclose(fp);
return 1;
}
}







0
Comment
Question by:tester090797
  • 5
  • 3
  • 2
  • +2
14 Comments
 
LVL 5

Accepted Solution

by:
laeuchli earned 1600 total points
ID: 1254566
I don't have to much time write now so I am only showing you a question I asked a while ago. IF you need more help post and I will keep on helping. Maybe you should use lex.
post to me.

0
 
LVL 5

Expert Comment

by:laeuchli
ID: 1254567
0
 
LVL 6

Expert Comment

by:thresher_shark
ID: 1254568
The question you pointed him to has very little to do with the question.  Also, shouldn't you leave it open so other people respond more?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:tester090797
ID: 1254569
No, this site that you're pointing me to will not help.
0
 
LVL 6

Expert Comment

by:thresher_shark
ID: 1254570
tester - Reject the current proposed answer so that the question will be more accessible to other users.  Questions with proposed answers tend to get little traffic.
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1254571
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>

int file_exists(char *filename);
main()
{
char ch, source[80];
int index;
long count[64];
int wordflag = 0;
int quotemode = 0;
long words=0;
FILE *fp;

fprintf(stderr, "\Enter source file name: ");
gets(source);

if(!file_exists(source))
{
fprintf(stderr, "\n%s does not exist.\n", source);
exit(1);
}
if((fp = fopen(source, "rb")) == NULL)
{
fprintf(stderr, "\nError opening %s.\n", source);
exit(1);
}

for(index = 21; index < 63; index++)
count[index] = 0;

while(1)
{
ch = fgetc(fp);
if(feof(fp))
break;
if((ch >= 21) && (ch < 63)) { count[ch]++; wordflag = 0; }
if(ch==34) quotemode = (!quotemode);
if((toupper(ch)>='A') && (toupper(ch) <= 'Z')  && (!wordflag) && (!quotemode)) {wordflag=1; words++;}
}

printf("\nChar\t\tCount\n");
for(index = 21; index < 42; index++)  {
  printf("[%c]\t%d    ", index, count[index]);
  printf("[%c]\t%d\n", (index+21), count[index+21]); }
printf("Words:  %d",words);

fclose(fp);
return(0);
}


int file_exists(char *filename)
{
FILE *fp;
if ((fp = fopen(filename, "r")) == NULL)
return 0;
else
{
fclose(fp);
return 1;
}
}

0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1254572
or do you need a concordance?
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1254573
Also note that I had to change your "fprint"s to "fprintf"s to compile on mine..I also had to change stlib.h to stdlib.h

If it doesn't compile on yours just switch them back.
0
 
LVL 5

Expert Comment

by:laeuchli
ID: 1254574
sorry that my answer did not help. I thought some of the soruce code and the info on lex and yacc might be helpfull. However, as it is not and there are quite a few people who look like they might have answer, feel free to reject mine.

0
 
LVL 11

Expert Comment

by:alexo
ID: 1254575
0
 

Author Comment

by:tester090797
ID: 1254576
Thank You, for the help, it has been awhile since I called on this service.
0
 
LVL 5

Expert Comment

by:laeuchli
ID: 1254577
If I did not help, how come you gave me the points?
Thanks jesse

0
 
LVL 11

Expert Comment

by:alexo
ID: 1254578
>> If I did not help, how come you gave me the points?
Consolation prize?
0
 
LVL 5

Expert Comment

by:laeuchli
ID: 1254579
I guess so :-)

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Suggested Courses

876 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question