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 :

6	|						
5	|						
4	|						
3	|						
2	|			*			
1	|			*			*
-	+	-	-	-	-	-	-
	|	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 :

#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();
}

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.

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.

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.

Comments
|*nods*

>Our teacher said we should use "gotoxy"
I laughed at this!

Care to explain? Does not even look like a joke to me!

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

if(c >= 'a' && c <= 'z')

it's much more portable to use

if(islower(c))
Comments
*nods*

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

Comments
not only blame, punish them
Creative dissin'
This article has been dead for over six months. Start a new discussion instead.