| | |
Reading from a file and counting how many time each letter occurs.
![]() |
•
•
Join Date: Oct 2007
Posts: 2
Reputation:
Solved Threads: 0
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 :
that's from A to F,obviously I need from A-Z.
This is what I have done so far :
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.
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 :
C Syntax (Toggle Plain Text)
6 | 5 | 4 | 3 | 2 | * 1 | * * - + - - - - - - | A B C D E F
This is what I have done so far :
c Syntax (Toggle Plain Text)
#include<stdio.h> #include<conio.h> #include<stdlib.h> FILE *a; char b; int count; int main() { clrscr(); a=fopen("C:\\text.txt","r"); while (1) { b=fgetc(a); if (b>64 && b<91) ++count if (b>97 && b<122) ++count if (b==EOF) break; } fclose(a); printf("letters %d",count); getch(); }
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 9:22 pm. Reason: Added CODE tags -- you actually typed right over how to use them when you entered this post...
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.
Unzip and run the attached program for an ASCII chart.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Last edited by Aia; Oct 20th, 2007 at 12:32 am.
"If it moves, tax it. If it keeps moving, regulate it, and if it stops moving, subsidize it" - Ronald Reagan stating how a liberal's mind works.
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().
'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
it's much more portable to use
•
•
•
•
if (b == "a" && b == "A") ++counta (this counts all "a"s and "A"s)
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
C Syntax (Toggle Plain Text)
if(c >= 'a' && c <= 'z')
C Syntax (Toggle Plain Text)
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
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
>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.
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 3:15 pm.
*Voted best profile in the world*
![]() |
Similar Threads
- Reading a file into a Parallel Array (C++)
- problems with reading in file (C++)
- First year assigment on reading file, sorting and outputting invoice (C++)
- Huge flat file (Visual Basic 4 / 5 / 6)
- Searching problem (C++)
- Vectors Versus Arrays (Java)
- Error Message Concerning Reading File From A Drive (C++)
- reading a file into code (Java)
Other Threads in the C Forum
- Previous Thread: problem in this code
- Next Thread: Sort strings in lexicographical order but not to use built-in string functions
Views: 1559 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for C
#include .net api array arrays binary binarysearch c++ char character code codeblocks coke command conversion convert curl database decimal dev-c++ directory dude dynamic error exec factorial fflush fgets file floatingpointvalidation fork function functions givemetehcodez grade graphics homework i/o include input insert int integer intmain() keyboard lazy line linked linkedlist linux list lists loop malloc matrix memory mysql no-effort output overwrite path pipe pointer pointers problem process processing program programming read recursion recursive recursiveloop recv reversal reverse scanf segmentationfault server simple socketprograming special spoonfeeding string strings strtok structures student subscript system testing tftp turbo-c turboc unbuffered unix user variable va_list windows






