Solved

word counting

Posted on 1998-11-22
14
215 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 400 total points
Comment Utility
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
Comment Utility
0
 
LVL 6

Expert Comment

by:thresher_shark
Comment Utility
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
 

Author Comment

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

Expert Comment

by:thresher_shark
Comment Utility
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
Comment Utility
#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
Comment Utility
or do you need a concordance?
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 5

Expert Comment

by:scrapdog
Comment Utility
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
Comment Utility
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
Comment Utility
0
 

Author Comment

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

Expert Comment

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

0
 
LVL 11

Expert Comment

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

Expert Comment

by:laeuchli
Comment Utility
I guess so :-)

0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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 and use conditional statements in the C programming language.

762 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now