Ir ao conteúdo
  • Cadastre-se

C preciso corrigir um erro!!!


Posts recomendados

achei esse código na internet que conta quantas letras tem na palavras, porém ta dando erro! 

entrada :macaco 

saída: 

m= 1

a= 2

c= 2

o= 1

 

 

#include <stdio.h>
#include <string.h>


#define STR_HISTOGRAM_MAX_TAM   (256)


void strhist( const char * str, int hist[ STR_HISTOGRAM_MAX_TAM ] )
{
    memset( hist, 0, sizeof(int) * STR_HISTOGRAM_MAX_TAM );

    while( *str )
        hist[ *str++ ]++;
}


void exibir_histograma( int hist[STR_HISTOGRAM_MAX_TAM], int min )
{
    int i = 0;
    int j = 0;

    for( i = 0; i < STR_HISTOGRAM_MAX_TAM; i++ )
    {
        if( (isprint(i)) && (hist >= min) )
        {
            printf("[%c]: %d ", i, hist );

            for( j = 0; j < hist; j++ )
                printf("*");

            printf("\n");
        }
    }
}


void inverso( const char * str )
{
    int i = 0;

    for( i = strlen(str) - 1; i >= 0; i-- )
        printf("%c", str );

    printf("\n");
}


int main( int argc, char ** argv )
{
    /* String original */
    char string[] = "Um pequeno jabuti xereta viu dez cegonhas felizes.";

    /* Histogama */
    int h[ STR_HISTOGRAM_MAX_TAM ];

    /* Exibe string original */
    printf("%s\n", string );

    /* Calcula histograma da string  */
    strhist( string, h );

    /* Exibe histograma das amostras com 2 ou mais ocorrencias */
    exibir_histograma( h, 2 );

    /* Exibe string invertida */
    inverso( string );

    return 0;
}

  • Obrigado 1
Link para o comentário
Compartilhar em outros sites

@João Paulo Pierot     para postar seu código é melhor usar o botão  <> ,

2071210336_barradeferramentas.jpg.86b93dd7259f5d1eed205b837ae24ff0.jpg

que está desenhado lá na barra de ferramentas da janela de postar , pois senão fica faltando alguns caracteres como os [ i ] , que sumiram do seu código , e   esse código não serve para contar letras não ,  ele apenas mostra alguns caracteres de algum local aleatório da memória , então seu código poderia ser assim     :

#include <stdio.h>
#include <conio.h>
#include <string.h>
#define STR_HISTOGRAM_MAX_TAM   (256)
void strhist( const char * str, int hist[ STR_HISTOGRAM_MAX_TAM ] ){
    memset( hist, 0, sizeof(int) * STR_HISTOGRAM_MAX_TAM );
    while( *str )
        hist[ *str++ ]++;
}
void exibir_histograma( int hist[STR_HISTOGRAM_MAX_TAM], int min ){
    int i,j;
    for(i=0; i<STR_HISTOGRAM_MAX_TAM; i++ ){
        if(( isprint(i) ) && ( hist[i] >= min )){
            printf("[%c]: %d ", i, hist[i] );
            for( j = 0; j < hist[i]; j++ )
                printf("*");
            printf("\n");
        }
    }
}
void inverso( const char * str ){
    int i;
    printf("\n");
    for(i=strlen(str) - 1; i>=0; i-- )
        printf("%c",str[i] );
    printf("\n");
}
int main( int argc, char ** argv ){
    /* String original */
    int j,i,tam=0,aux_2,
        k[1024],cont= -1;
    char string[] = "Um pequeno jabuti xereta viu dez cegonhas felizes.",
         btr[strlen(string)],copia_string[strlen(string)],aux;
         strcpy(copia_string,string);
    for(j=0; j<strlen(copia_string); j++){
        k[cont+1]=0;
        if(copia_string[j] != '1'){
            cont++;
            btr[cont]=copia_string[j];
            k[cont]=1;
            tam++;
        }
        for(i=j; i<strlen(copia_string); i++){
            if(copia_string[i] == copia_string[j] && copia_string[j] !='1' && i != j){
                ;copia_string[i] = '1';
                k[cont]++;
            }
        }
    }
    // um bubbleSort
    for(i=0; i<tam; i++         )
        for(j=i; j<tam; j++     )
            if(btr[i]  > btr[j]){
                ;aux   = btr[i] ;
                btr[i] = btr[j] ;
                btr[j] = aux    ;
                aux_2  = k[i]   ;
                k[i]   = k[j]   ;
                k[j]   = aux_2  ;
            }
    printf("\nMostrando por Ordem Alfabetica com Maiusculas em primeiro :\n\n");
    for(j=0; j<tam; j++)
        printf("[ %c ] : %d\n",btr[j],k[j]);
    printf("\n\n");
    return 0;
    /* Histogama */
    int h[ STR_HISTOGRAM_MAX_TAM ];
    /* Exibe string original */
    printf("%s\n\n",string);
    /* Calcula histograma da string  */
    strhist( string, h );
    /* Exibe histograma das amostras com 2 ou mais ocorrencias */
    exibir_histograma( h, 2 );
    /* Exibe string invertida */
    inverso( string );
    return 0;
}

 

Link para o comentário
Compartilhar em outros sites

Crie uma conta ou entre para comentar

Você precisa ser um usuário para fazer um comentário

Criar uma conta

Crie uma nova conta em nossa comunidade. É fácil!

Crie uma nova conta

Entrar

Já tem uma conta? Faça o login.

Entrar agora

Sobre o Clube do Hardware

No ar desde 1996, o Clube do Hardware é uma das maiores, mais antigas e mais respeitadas comunidades sobre tecnologia do Brasil. Leia mais

Direitos autorais

Não permitimos a cópia ou reprodução do conteúdo do nosso site, fórum, newsletters e redes sociais, mesmo citando-se a fonte. Leia mais

×
×
  • Criar novo...