hello everyone i need help with an assignemnt that askes me to read from a file and count the number of occurence of each letter in the alphabet . im stuck trying to group each letter in an array. i think using a switch statement would be the easiest way but dont know how to make it happen

#include <stdio.h>
#define MAX_FILE 1000000

int readFile(char fileName[], char textStr[]);

int main(int argc, char *argv[]){

    char textStr[MAX_FILE]; 
    char cArray[27] = '\0';

    if (argc != 2){
        printf("Invalid number of arguments\n");
        printf("Usage: %s file_name\n", argv[0]);
        return 1;
    }

    if (readFile(argv[1], textStr) == -1) {
        printf("Failed to load file\n");
        return 1;
    }

        int i=0;
        while(textStr[i]!= '\0')
        {
        printf("%c", textStr[i]);
        i++;
      }

    switch(){

    case 'a': case 'A':

         /*code here */
    }

        iArray[i];

    return 0;
}

int readFile(char fileName[], char textStr[])
{
    FILE *fptr;
    char c;
    int i = 0;

    if ((fptr = fopen(fileName, "r")) == NULL){
        printf("Error: failed to open %s\n", fileName);
        return -1;
    }

    while ((c = fgetc(fptr)) != EOF) {
        if (i == MAX_FILE - 1)
            break;
        textStr[i++] = c;
    }

    textStr[i] = '\0';

    return i;   
}

Recommended Answers

All 8 Replies

something like that could be design m?

#include "stdio.h"
#include "stdlib.h"
#define APLPABET_LEN 2//should be 25

struct stAlpha
{
   int iCount;
   char cChar;
};
enum
{
   CHAR_A = 0,
   CHAR_B = 1,//add needed indexes
};

void vPrintResultAlpha(stAlpha stAlphabet[])
{
   int i = 0;
   for (i; i < APLPABET_LEN; i++)
   {
      printf("char %c count %d\n",stAlphabet[i].cChar, stAlphabet[i].iCount);
   }
   printf("##############\n");
   return;
}
int main()
{
   stAlpha stAlphabet[APLPABET_LEN] = {{0, 'A'}, {0, 'B'}};//initialise 25 chars since we know alphabet
   int i = 0;

   vPrintResultAlpha(stAlphabet);

   stAlphabet[CHAR_B].iCount++;//found char 'b' or 'B' ?
   //check with if ()
   //{
   //}
   //else if()
   //{
   //}
   //else if()
   //{
   //}
   vPrintResultAlpha(stAlphabet);
   return 0;
}

or if you understand good you can read char x and use stAlphabet[x - 65].iCount++;
ofcourse check for needed range and toUpper then
you can refere to ascii table http://www.cdrummond.qc.ca/cegep/informat/Professeurs/Alain/images/ASCII1.GIF

you can read char by char by 1, or 50 then check this buffer or more
first try to printf what you need to check to see that you have needed data

Each character has a numeric value. 'A'=65, 'B'=66, 'a'=97, etc. There are 256 possible values.

So read a character. Use the character as an index into stAlphabet[256] and increment that value. When you hit the EOF, you will have the count of all characters in the file.

@WaltP
wow, nice idea :) except that he needs only small letters or only big letters in case he gets 3 'a' and 5'A'

Yeah, it's quite impossible after collecting all the letter counts to figure out how to combine the counts for 'a' and 'A'...
(i wish we still had the roll-eyes -- i like to mark sarcasm in case Sheldon Cooper is reading this...)

@WaltP

Yeah, it's quite impossible after collecting all the letter counts to figure out how to combine the counts for 'a' and 'A'...
(i wish we still had the roll-eyes -- i like to mark sarcasm in case Shedon Cooper is reading this...)

didn't i just say that he needs to do it, where did i say it is not possible ? ?

except that he needs only small letters or only big letters in case he gets 3 'a' and 5'A'

I think you've missed WaltP's point, which was that the counts for each upper and lower case value could simply be added together after the fact.

@Schol-R-LEA
I didnt mention when to do it in response to WaltP

didn't i just say that he needs to do it, where did i say it is not possible ? ?

Sorry, Sokurenko, if that's what you meant. The way it reads is by calculating all the values and he only needs 'A' and 'a' added my suggestion is cumbersome and prone to not working.

But then again, isn't it obvious? I'm somewhat old school and rather than tell people book:chapter:verse:word, I give them basic information and allow them to think about how to implement it. I wanted reyesg to figure the addition out himself...

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.