histogram of a c++ phrase

Asked

Viewed 529 times

1

I made the following program to make the histogram of a sentence counting the number of letters and consonants in it but this giving error because I’m trying to get him to print capital letters and minuscules marked or not..

#include <cstdio>

void histogram(char *);

int main()
{
    char str[50];

    printf("\n\tDigite uma frase: ");
    fgets(str,50,stdin);
    histogram(str);

}

void histogram(char *frase)
{
  int num=0, az=0;
  char alfabeto[]="abcdefghijklmnopqrstuvwxyzçABCDEFGHIJKLMNOPQRSTUVWXYZÇ";

   for(num = 0; frase[num] != 0; num++)
    {
     if(frase[num] <= alfabeto[num])az++;
    }

   printf
    (
     "\n\n\tTemons %d letras \"%s\"\n\n"    
     , az, alfabeto[az]
     );
}

1 answer

1


The comparison you have in the for:

if(frase[num] <= alfabeto[num])az++;

It doesn’t count the letters correctly, because it’s only comparing if the letter you have is smaller than the letter you go to in the alphabet, and it doesn’t exist in the alphabet.

Yet the mistake you’re getting comes from the last printf in:

 printf
 (
 "\n\n\tTemons %d letras \"%s\"\n\n"    
 , az, alfabeto[az] //<-- aqui
 );

For it prints a %s with a letter and not an address. It is important to remember that a parameter of type %s expecting a char* and not a char, which is the case with alfabeto[az]. You can pass the address of the letter with &alfabeto[az], but still I don’t see the point of doing it.

To count the letters that are part of the alphabet defined by you then you need a second for running through the alfabeto and check that the current letter matches that of alfabeto.

I suggest you do so:

#include <stdio.h>

void histogram(char *);

int main() {
    char str[50];

    printf("\n\tDigite uma frase: ");
    fgets(str,50,stdin);
    histogram(str);
    return 0;
}

void histogram(char *frase) {
    int num=0, num2=0, az=0;
    char alfabeto[]="abcdefghijklmnopqrstuvwxyzçABCDEFGHIJKLMNOPQRSTUVWXYZÇ";

    for(num = 0; frase[num] != 0; num++) {
        for (num2 = 0; alfabeto[num2] != '\0'; ++num2){
            if (frase[num] == alfabeto[num2]){
                az++;
                break;
            }
        }
    }

    printf("\n\n\tTemons %d letras\n\n", az);
}

Example in Ideone

Notice also that I changed the include for a suitable C instead of C++.


Edit:

If you want to know the count of each letter, you need to count for an array for each font. The very display of values will have to be done with a for since it will have a value for each letter that appears.

Example:

void histogram(char *frase) {
    int num=0, num2=0, az=0;
    char alfabeto[]="abcdefghijklmnopqrstuvwxyzçABCDEFGHIJKLMNOPQRSTUVWXYZÇ";
    int contagens[256]; //array para as contagens das letras
    for (num= 0; num < 256; ++num){
        contagens[num]=0; //iniciar o array todo a zeros
    }

    for(num = 0; frase[num] != 0; num++) {
        for (num2 = 0; alfabeto[num2] != '\0'; ++num2){
            char letra = frase[num];
            if (letra == alfabeto[num2]){
                contagens[(int)letra]++; //contabilizar para a letra corrente
                az++;
                break;
            }
        }
    }

    printf("\n\n\tTemons %d letras\n\n", az);
    for (num = 0; num < 256; ++num){
        if (contagens[num] > 0){ //mostrar apenas os que tem pelo menos 1 letra
            printf("\n%c: %d", (char)num, contagens[num]);
        }
    }
}

See this example also in Ideone

To start the zeros count array you can alternatively use the function memset, making memset(contagens, 0, sizeof(contagens));, however implies including string.h.

  • it is showing the amount of letters existing in the sentence what I want it to show for ex: dark777 has 1 letter d, 1 letter a, 1 letter r, 1 letter k....

  • @dark777 I edited the answer to contemplate this logic.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.