Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

C: output most frequent word

Name: ludalex 2012-04-23 13:11

[HOMEWORK]
Hello, I have to make a C program to output the most used letter in a given string.

Here's what I've accomplished so far. It works fine, I guess, but I think there's a MUCH better way to write this algorythm.
Thanks in advance.


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

struct alphabet {
    char letter;
    int times;
};

typedef struct alphabet Alphabet;

void main() {

char string[300];

    gets(string);


Alphabet Alph[300];

int i=0, j=0;

while (i<=strlen(string)) {

    while(j<=300) {

    if(string[i] != Alph[j].letter) {

        Alph[i].letter = string[i];
        Alph[i].times = 1;


    }
    else {

        Alph[j].times++;

        }
        j++;
        }

    j=0;
    i++;
    }



int y,max=0;
char letter_max[0];
for (y=0; y<strlen(string); y++) {

printf("Letter: %c, Times: %d \n", Alph[y].letter, Alph[y].times);

    if(Alph[y].times>max) {

        max=Alph[y].times;
        letter_max[0]=Alph[y].letter;
        }


}

printf("\n\n\t\tMost frequent letter: %c - %d times \n\n", letter_max[0], max);


}

Name: Anonymous 2012-04-24 1:34

#include <stdio.h>
#include <stdlib.h>
void main(){
    char string[300]; gets(string); int i = 0; int times[26];
   
    while(i<strlen(string)){
        switch (string[i]){
            case 'a': times[0]++;break; case 'A':times[0]++;break;
            case 'b': times[1]++;break; case 'B':times[1]++;break;
            case 'c': times[2]++;break; case 'C':times[2]++;break;
            case 'd': times[3]++;break; case 'D':times[3]++;break;
            case 'e': times[4]++;break; case 'E':times[4]++;break;
            case 'f': times[5]++;break; case 'F':times[5]++;break;
            case 'g': times[6]++;break; case 'G':times[6]++;break;
            case 'h': times[7]++;break; case 'H':times[7]++;break;
            case 'i': times[8]++;break; case 'I':times[8]++;break;
            case 'j': times[9]++;break; case 'J':times[9]++;break;
            case 'k': times[10]++;break; case 'K':times[10]++;break;
            case 'l': times[11]++;break; case 'L':times[11]++;break;
            case 'm': times[12]++;break; case 'M':times[12]++;break;
            case 'n': times[13]++;break; case 'N':times[13]++;break;
            case 'o': times[14]++;break; case 'O':times[14]++;break;
            case 'p': times[15]++;break; case 'P':times[15]++;break;
            case 'q': times[16]++;break; case 'Q':times[16]++;break;
            case 'r': times[17]++;break; case 'R':times[17]++;break;
            case 's': times[18]++;break; case 'S':times[18]++;break;
            case 't': times[19]++;break; case 'T':times[19]++;break;
            case 'u': times[20]++;break; case 'U':times[20]++;break;
            case 'v': times[21]++;break; case 'V':times[21]++;break;
            case 'w': times[22]++;break; case 'W':times[22]++;break;
            case 'x': times[23]++;break; case 'X':times[23]++;break;
            case 'y': times[24]++;break; case 'Y':times[24]++;break;
            case 'z': times[25]++;break; case 'Z':times[25]++;break;
            default: break;
        }
        i++;
    }
    int max=0;maxchar=0;
   
    for(i=0; i<26; i++){
        printf("Letter %c of the alphabet appears %d times.\n", i, times[i]);
        if(tmes[i]>max){ max=times[i];maxchar=i; }
    }
    printf("\n\n\t\tThe %c numbered letter of the alphabet is most frequent, appearing %d times \n\n", maxchar, max);
}

Name: Anonymous 2012-04-24 2:15

Converting the numeral of a letter to a character in your output string is an excersize left for the reader.

Name: Anonymous 2012-04-24 3:29

>>1
As for your implementation I have this to say.
There shouldn't be a need to have a class to handle the alphabet be 300 characters unless you really want to account for 300 distinct characters which would be alphanumeric+the rest.

The other thing is in your implementation you have no handling of the case, so in effect each of the two forms of the letters are competing against with each other and the other (two forms) of letters instead of being considered together, ultimately this will mean your output will be wrong.

For example in your string
a: appears once
A: appears 99 times
b: appears 50 times
B: appears 51 times

Sure, the uppercase A is the sole character that appears the most, but in fact the actual letter of b which includes both cases appears together one more time than the letter of a.

Unless somehow you aren't supposed to include the cases together, uppercase or lowercase, the letter a is still the letter a.

Also since it doesnt do any distinct character checking, you can have a string where a non letter is the most frequent 'letter'. Which as far as I can tell, would be contrary to the purpose of the program.

However this kind of approach can be useful if what you want to know the frequency of isn't strictly letters but any kind of distinct character in the string.

Name: ludalex 2012-04-24 8:09

>>2
Doesn't work.
>>4
Yes, you're totally right.

Anyway, here's a different implementation:

void main()
{

int array[255] = {0}; // initialize all elements to 0

char str[] = "thequickbrownfoxjumpedoverthelazydog";

int i, max, index;

for(i = 0; str[i] != 0; i++)
{
   ++array[str[i]];
}


// Find the letter that was used the most
max = array[0];
index = 0;
for(i = 0; str[i] != 0; i++)
{
     if( array[str[i]] > max)
     {
         max = array[str[i]];
         index = i;
     }
}

printf("The max character is: %c \n", str[index]);

}

Name: Anonymous 2012-04-24 15:34

>>5
It doesn't?
>>5
And this does? How can you use a character as an index value exactly? I thought you could only use positive integers for a possible value there?
   ++array[str[i]]; str[i] is a character in the string, not an integer, have I been trolled? I'm confused.
Well if it works then congrats, I'll assume you've learned some things and completed your assignment in time.

Name: ludalex 2012-04-24 18:49

>>6
It works because str[i] going into array[] is a number, the number corresponding to the letter (according to ASCII standards).

Name: Anonymous 2012-04-24 22:43

>>7
Interesting, I didn't know C could actually dynamically typecast it like that.

Don't change these.
Name: Email:
Entire Thread Thread List