Reading from a file and counting how many time each letter occurs.

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2007
Posts: 2
Reputation: Alexbeav is an unknown quantity at this point 
Solved Threads: 0
Alexbeav Alexbeav is offline Offline
Newbie Poster

Reading from a file and counting how many time each letter occurs.

 
0
  #1
Oct 19th, 2007
Hi all I'm studying C and I have to do an exercise that requires me to open a file and count the number of times a letter occurs,either small or capital.
For example,I have a text file in C:\text.txt with the contents "World In Conflict".So in my example the letter "c" is counted twice.After I have finished counting the letters,I have to make
a graphical representation that's to be like this :
  1. 6 |
  2. 5 |
  3. 4 |
  4. 3 |
  5. 2 | *
  6. 1 | * *
  7. - + - - - - - -
  8. | A B C D E F
that's from A to F,obviously I need from A-Z.

This is what I have done so far :
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<stdlib.h>
  4.  
  5. FILE *a;
  6. char b;
  7. int count;
  8.  
  9. int main()
  10. {
  11. clrscr();
  12. a=fopen("C:\\text.txt","r");
  13.  
  14. while (1)
  15. {
  16. b=fgetc(a);
  17. if (b>64 && b<91) ++count
  18. if (b>97 && b<122) ++count
  19. if (b==EOF)
  20. break;
  21. }
  22. fclose(a);
  23. printf("letters %d",count);
  24. getch();
  25. }
I thought using ASCII would be a good idea,but how can I specifically count the letters using ASCII ? I could do it using

if (b == "a" && b == "A") ++counta (this counts all "a"s and "A"s)

for all letters,but I don't think that's the point of this exercise.

Also,how do I design the GUI ? Our teacher said we should use "gotoxy" but I haven't been able to get a handle on it.

Any help would be much appreciated.
Last edited by WaltP; Oct 19th, 2007 at 10:22 pm. Reason: Added CODE tags -- you actually typed right over how to use them when you entered this post...
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,127
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 283
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Reading from a file and counting how many time each letter occurs.

 
0
  #2
Oct 19th, 2007
Each letter (in fact each character) has a specific numeric value. Each character can therefore be used as an index into an array in which you can increment the appropriate element. Then run through the array when done and output the values you need.

Unzip and run the attached program for an ASCII chart.
Attached Files
File Type: zip charlist.zip (14.0 KB, 6 views)
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,048
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 179
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Reading from a file and counting how many time each letter occurs.

 
1
  #3
Oct 20th, 2007
Originally Posted by Alexbeav View Post
Hi all I'm studying C and I have to do an exercise that requires me to open a file and count the number of times a letter occurs,either small or capital.
There's a conversation about the same topic over here. Maybe it might help.
Take a look at this also.
Last edited by Aia; Oct 20th, 2007 at 1:32 am.
"If it moves, tax it. If it keeps moving, regulate it, and if it stops moving, subsidize it" - Ronald Reagan
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Reading from a file and counting how many time each letter occurs.

 
0
  #4
Oct 20th, 2007
>Our teacher said we should use "gotoxy"
I laughed at this!
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 1,553
Reputation: Lardmeister is an unknown quantity at this point 
Solved Threads: 22
Lardmeister's Avatar
Lardmeister Lardmeister is offline Offline
Posting Virtuoso

Re: Reading from a file and counting how many time each letter occurs.

 
0
  #5
Oct 20th, 2007
Originally Posted by iamthwee View Post
>Our teacher said we should use "gotoxy"
I laughed at this!
Care to explain? Does not even look like a joke to me!
I upped my sanitary measures, up yours!
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 251
Reputation: dwks has a spectacular aura about dwks has a spectacular aura about 
Solved Threads: 25
dwks's Avatar
dwks dwks is offline Offline
Posting Whiz in Training

Re: Reading from a file and counting how many time each letter occurs.

 
1
  #6
Oct 20th, 2007
gotoxy() is an ancient Borland function. It's very unportable. No new code should use it or anything from <conio.h>, such as getch() or clrscr().

if (b == "a" && b == "A") ++counta (this counts all "a"s and "A"s)
'a' is a single character. "a" is a string. Use single quotes if you mean a single character.

The functions from <ctype.h> will make your life easier. There are such functions as tolower(), which converts a letter to lowercase, and isalpha(), which returns true if a character is a letter. These functions are also portable, so even if you could use
  1. if(c >= 'a' && c <= 'z')
it's much more portable to use
  1. if(islower(c))
dwk

Seek and ye shall find.

"Only those who will risk going too far can possibly find out how far one can go."
-- TS Eliot.

"I have not failed. I've just found 10,000 ways that won't work."
-- Thomas Alva Edison

"The only real mistake is the one from which we learn nothing."
-- John Powell
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Reading from a file and counting how many time each letter occurs.

 
2
  #7
Oct 21st, 2007
>Care to explain? Does not even look like a joke to me!

Yes the point of the matter is that the same output can be recreated without gotoxy() which is non-portable - meaning it won't work on some compilers.

Anyone writing code should strive to make their solutions portable and as close to what the standard dictates, unless there is a very good reason for doing otherwise. In most cases there isn't. The people who primarily use gotoxy() are ignorant of the portability issues although there are exceptions.

Unfortunately we have to blame the pedagogues for teaching the subject with the, oh so notoriously infamous, turbo c compiler. Where students feel predisposed to use clrscr(), gotoxy() getch() and all the other misnomers.

An even greater crime perhaps, are the teachers who teach c++ with the old turbo c compiler, but I'll leave that little bedtime story for another occasion.
Last edited by iamthwee; Oct 21st, 2007 at 4:15 pm.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1469 | Replies: 6
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC