We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,445 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Need help with program

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;   
}
4
Contributors
8
Replies
20 Hours
Discussion Span
1 Year Ago
Last Updated
9
Views
reyesg
Newbie Poster
3 posts since May 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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

Sokurenko
Junior Poster
111 posts since May 2012
Reputation Points: 42
Solved Threads: 13
Skill Endorsements: 0

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
Posting Sage w/ dash of thyme
Team Colleague
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 37

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

Sokurenko
Junior Poster
111 posts since May 2012
Reputation Points: 42
Solved Threads: 13
Skill Endorsements: 0

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
Posting Sage w/ dash of thyme
Team Colleague
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 37

@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'

Sokurenko
Junior Poster
111 posts since May 2012
Reputation Points: 42
Solved Threads: 13
Skill Endorsements: 0

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
Veteran Poster
1,089 posts since Oct 2010
Reputation Points: 495
Solved Threads: 173
Skill Endorsements: 13

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

Sokurenko
Junior Poster
111 posts since May 2012
Reputation Points: 42
Solved Threads: 13
Skill Endorsements: 0

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

WaltP
Posting Sage w/ dash of thyme
Team Colleague
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 37

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page generated in 0.3170 seconds using 2.7MB