943,884 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 2126
  • C RSS
Oct 19th, 2007
0

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

Expand 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.
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...
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Alexbeav is offline Offline
2 posts
since Oct 2007
Oct 19th, 2007
0

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

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, 47 views)
Moderator
Reputation Points: 3278
Solved Threads: 892
Posting Sage
WaltP is offline Offline
7,718 posts
since May 2006
Oct 20th, 2007
1

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

Click to Expand / Collapse  Quote originally posted by Alexbeav ...
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.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Oct 20th, 2007
0

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

>Our teacher said we should use "gotoxy"
I laughed at this!
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Oct 20th, 2007
0

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

Click to Expand / Collapse  Quote originally posted by iamthwee ...
>Our teacher said we should use "gotoxy"
I laughed at this!
Care to explain? Does not even look like a joke to me!
Reputation Points: 407
Solved Threads: 36
Posting Virtuoso
Lardmeister is offline Offline
1,701 posts
since Mar 2007
Oct 20th, 2007
1

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

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().

Quote ...
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))
Reputation Points: 185
Solved Threads: 28
Posting Whiz in Training
dwks is offline Offline
269 posts
since Nov 2005
Oct 21st, 2007
2

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

>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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: problem in this code
Next Thread in C Forum Timeline: Sort strings in lexicographical order but not to use built-in string functions





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC