Solved

word counting

Posted on 1998-11-22
14
224 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +2
14 Comments
 
LVL 5

Accepted Solution

by:
laeuchli earned 400 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
Technology Partners: 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!

 

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

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

696 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