Solved

word counting

Posted on 1998-11-22
14
221 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
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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

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…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

813 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

12 Experts available now in Live!

Get 1:1 Help Now