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.

Dani AI

Generated

A few practical corrections and a compact, portable approach.

Your original snippet has several bugs: fgetc() returns an int (so store it in an int and compare with EOF), counters must be initialized, character literals use single quotes ('a') not double quotes ("a"), and the ASCII-range checks were fragile. As suggested, use the character value as an array index; as noted, ctype.h helpers (isalpha/tolower) make this easier — but remember to cast to (unsigned char) before calling those functions. conio.h/gotoxy() are non‑portable (see comments by and ); prefer plain output, ncurses/PDCurses for console control, or a GUI toolkit for real windows.

Below is a small, portable C program that counts A–Z case‑insensitively and prints a vertical ASCII histogram similar to your example. It checks fopen, avoids conio.h, and demonstrates correct fgetc/EOF handling.

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int main(void) {
    const char *path = "C:\\text.txt";
    FILE *f = fopen(path, "r");
    if (!f) { perror(path); return 1; }

    long counts[26] = {0};
    int ch;
    while ((ch = fgetc(f)) != EOF) {
        if (isalpha((unsigned char)ch)) {
            ch = tolower((unsigned char)ch);
            counts[ch - 'a']++;
        }
    }
    fclose(f);

    long max = 0;
    for (int i = 0; i < 26; ++i) if (counts[i] > max) max = counts[i];

    for (long row = max; row > 0; --row) {
        printf("%3ld |", row);
        for (int i = 0; i < 26; ++i)
            printf(counts[i] >= row ? " * " : "   ");
        printf("\n");
    }
    printf("    +");
    for (int i = 0; i < 26; ++i) printf("---");
    printf("\n     ");
    for (int i = 0; i < 26; ++i) printf(" %c ", 'A' + i);
    printf("\n");

    return 0;
}

Notes and troubleshooting

  • Always check fopen and fclose.
  • Initialize counters (above uses {0}).
  • Cast to (unsigned char) before isalpha/tolower to avoid undefined behavior on negative char.
  • If you need cursor positioning, use ncurses/PDCurses or a GUI toolkit (Win32/GTK/Qt) rather than gotoxy(), which is compiler/OS specific.
  • To test with "World In Conflict" you should see C counted twice; adjust the path or run from the folder containing text.txt.

Recommended Answers

All 6 Replies

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.

commented: |*nods* +11
Member Avatar for Member #46692

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

>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 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))
commented: *nods* +11
Member Avatar for Member #46692

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

commented: Creative dissin' +11
commented: not only blame, punish them +4
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.