Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8929415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:40:04+00:00 2026-06-15T08:40:04+00:00

I have written a program in C running on UNIX which counts the number

  • 0

I have written a program in C running on UNIX which counts the number of each letters in a input text file. For a file like this:

‘The cat sat on the green mat’

The output would be like this:

   The letter ’a’ occurs 3 times.
   The letter ’c’ occurs 1 times.
   The letter ’e’ occurs 4 times.
   The letter ’g’ occurs 1 times.
   The letter ’h’ occurs 2 times.
   The letter ’m’ occurs 1 times.
   The letter ’n’ occurs 2 times.
   The letter ’o’ occurs 1 times.
   The letter ’r’ occurs 1 times.
   The letter ’s’ occurs 1 times.
   The letter ’t’ occurs 5 times.

  5                    *
  4     *              *
  4     *              *
  3 *   *              *
  3 *   *              *
  2 *   *  *     *     *
  2 *   *  *     *     *
  1 * * * **    ***  ***
  1 * * * **    ***  ***
  0 **************************
  0 **************************
... abcdefghijklmnopqrstuvwxyz

Where the graph represents the amount of times a letter appears. (If it is more than 10, i simply put a ‘+’ after the 10th row). The code I’ve currently written to achieve this is as follows:

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

void drawGraph(int letters[26], char alpha[26]);
void printLetters(int letters[26], char alpha[26]);
void getLetters(FILE *fp, int letters[26], char alpha[26]);

int main(int argc, char *argv[]) {
  FILE *fp;
  int letters[26] = { 0 };
  char alpha[26] = { 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z' };
  int indexedAlpha[256] = { 0 };
  int j = 1;

  for (i = 97; i <= 127; i++)
    {
      indexedAlpha[i] = j;
      j++;
    }

  //open file
  if ((fp = fopen(argv[1], "r")) == NULL)
    {
      perror("Cannot open file");
      exit(EXIT_FAILURE);
    }

  getLetters(fp, letters, alpha);
  printLetters(letters, alpha);
  printf("\n");
  drawGraph(letters, alpha);
  printf("\n");

  return EXIT_SUCCESS;
}

void getLetters(FILE *fp, int letters[26], char alpha[26]) {
  int c;
  for (int i = 0; (c = fgetc(fp)) != EOF; i++)
    {
      c = fgetc(fp);
      if ( isalpha(c) )
    {
      for ( int j = 0; j < 26; j++ ) //find which letter it is
        {
          if( c == alpha[j] ) 
        {
          letters[j]++;
          break;
        }
        }
    }
    }
}

void printLetters(int letters[26], char alpha[26]) {
  for( int i = 0; i < 26; i++ )
    {
      if(letters[i] != 0){
    printf("The letter '%c' occurs %d times.\n", alpha[i], letters[i]);
      }
    }
}

void drawGraph(int letters[26], char alpha[26]) {
  int x = 11;
  int y;
  while(x >= 0)
    { 
      y = 0;
      while (y < 2)
    {
      if (x == 10)
        {
          printf(" %d ", x);
        }
      else if (x == 11)
        {
          printf("    ");
        }
      else
        {
          printf("  %d ", x);
        }

      for( int i = 0; i < 26; i++ )
        {
          if(letters[i] > 10)
        {
          printf("+");
          letters[i] = 10;
          y++; // Break out of while loop
        }
          else if(letters[i] == x)
        {
          printf("*");
        }
          else
        {
          printf(" ");
        }
          if (letters[i] == x && y == 1)
        {
          letters[i] = letters[i] - 1;
        }
        }
      printf("\n");
      y++;
    }
      x--;
    }
  printf("... ");

  for( int i = 0; i < 26; i++ )
    {
      printf("%c", alpha[i]);
    }
}

However my current code has two problems.
1. I always print out 10 Y-axis points i want to only print out as many Y-axis points as needed, What would be the best way to achieve this?
2. Currently only lower case characters are counted, how can i address this?

Also any notation or better methodology would be much appreciated, Im still trying to learn!

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T08:40:04+00:00Added an answer on June 15, 2026 at 8:40 am

    2nd question seems bit easy to me :

    2. Currently only lower case characters are counted, how can i address this?
    

    make an structure like this :

        typedef struct 
        {
         char c; 
         int count;
        }alpha;
    
        alpha abc[26];
    
    for(i=0 ; i<26 ; i++)
      abc[i].count = 0; // Initialization of count for each alphabet
    for(i=0;i<26; i++)
      abc[i].c = 'a' + i;
    

    In this way you can keep track of each letter with it’s count.

    To print “histogram” of each alphabet in file you need (11 + 1 + 1) lines

    (means at least 13 lines , can be extra lines for separating characters and their bar )

    11 lines for 11 times occurrence of alphabet and 1 for + as mentioned and 1 for alphabets itself.

    But to print these lines, You need to take care of extra things about the space before the each of the *(histogram symbol you used).

    So, loop through the array and print it like, For your 1st question try something like this:

    int cnt;
    for(cnt = 11 ; c >=0 ; c--)
    {
     for(i=0; i<26; i++)
     {
      if(abc[i].count >= cnt && cnt == 11)  
      {
       space = abc[i].c - 'a';
       printf("%*c",space,'+'); // setting indentation and printing
      }
      if(abc[i].count == cnt && cnt != 11)  
      {
       space = abc[i].c - 'a';
       printf("%*c",space,'*');  //// setting indentation and printing
      }
      printf("\n");
     } //end of inner for loop
    } // end of outer for loop 
    
    printf("abcdefghijklmnopqrstuvwxyz\n");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written this program, which sorts some ints using a functor: #include<iostream> #include<list>
i have written a c program to which i pass in a script like
I have a program written in C and running on Linux which acquires streaming
Ok, what we have: Program written on C which compiling and running without problems
I have a .NET CF program (running on a Smartphone) and I have written
I have written php program and uploaded on server. I want run this program
I have written a program that uses qhttp to get a webpage. This works
I have written a program a.exe which launches another program I wrote, b.exe ,
I have a program written in C#, running on a Windows CE device (on
I have written a program which triggers a relay switch on a serial port.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.