histogram of a c ++ sentence

1

I made the following program to make the histogram of a sentence counting the number of letters and consonants in the same one but it is giving error because I'm trying to get it to print uppercase and lowercase letters accented 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]
     );
}
    
asked by anonymous 09.12.2017 / 15:25

1 answer

1

The comparison you have in for :

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

You do not count the letters correctly because you are only comparing if the letter you have is less than the letter you are in the alphabet, and it does not exist in the alphabet.

However the error you are getting comes from the last printf in:

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

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

To count the letters that are part of the alphabet you define, you need a second for that runs through alfabeto and verifies that the current letter coincides with 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] != '
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] != '
if(frase[num] <= alfabeto[num])az++;
'; ++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]); } } }
'; ++num2){ if (frase[num] == alfabeto[num2]){ az++; break; } } } printf("\n\n\tTemons %d letras\n\n", az); }

Example on Ideone

Note also that I've changed include to an appropriate C instead of C ++.

Edit:

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

Example:

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

See this example also in Ideone

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

    
09.12.2017 / 20:47