Solved

word counting

Posted on 1998-11-22
14
217 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
 

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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to declare a 2-dimensional version of strlen 4 202
how to understand recursion 12 219
Intel debugger, idb, only seeing .c and .h, but no .f90 1 299
Resolve Dependency Issues 4 48
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…
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…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them 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.

863 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

21 Experts available now in Live!

Get 1:1 Help Now