Program to discover repeated characters in strings and print backwards

1

I need to find out how many and which repeated characters exist in a string (after informing it). And also print this string backwards. I'm in big trouble. Here is part of the code I have made so far, but with several errors.

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

#define TAM 100

main () {       
    char palavra[TAM], c;
    int i = 0, aux = 0, contador = 0;

    printf("Digite uma palavra: \n" );
    while ((c=getchar())!= '\n'){
        palavra[i]=c;
        i++;        
    }

    palavra[i] = '
#include <stdio.h>
#include <stdlib.h>

#define TAM 100

main () {       
    char palavra[TAM], c;
    int i = 0, aux = 0, contador = 0;

    printf("Digite uma palavra: \n" );
    while ((c=getchar())!= '\n'){
        palavra[i]=c;
        i++;        
    }

    palavra[i] = '%pre%';

    i = 0;

    while (palavra[i]!= '%pre%'){                  
        if(palavra[i]==palavra[i]){
            contador++;
        }                              

        printf ("%c = %d \n", palavra[i], contador);  
        i++;
    }
}   
'; i = 0; while (palavra[i]!= '%pre%'){ if(palavra[i]==palavra[i]){ contador++; } printf ("%c = %d \n", palavra[i], contador); i++; } }
    
asked by anonymous 23.01.2018 / 19:39

4 answers

1

If you want to count how many letters repeat and how many times, we can use an auxiliary array as an occurrence counter to help. Here is the code:

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

#define TAM 100
#define MAX_CHARS 256

int main() {       
    char palavra[TAM];
    int repeticoes[MAX_CHARS];
    int i, tamanho = 0;

    printf("Digite uma palavra: \n" );
    while (tamanho < TAM - 1 && (c = getchar()) != '\n') {
        palavra[tamanho] = c;
        tamanho++;
    }
    palavra[tamanho] = '
#include <stdio.h>
#include <stdlib.h>

#define TAM 100
#define MAX_CHARS 256

int main() {       
    char palavra[TAM];
    int repeticoes[MAX_CHARS];
    int i, tamanho = 0;

    printf("Digite uma palavra: \n" );
    while (tamanho < TAM - 1 && (c = getchar()) != '\n') {
        palavra[tamanho] = c;
        tamanho++;
    }
    palavra[tamanho] = '%pre%';

    for (i = 0; i < tamanho; i++) {
        repeticoes[palavra[i]]++;
    }

    for (i = 0; i < MAX_CHARS; i++) {
        if (repeticoes[i] > 0) {
            printf("%c = %d\n", (char) i, repeticoes[i]);
        }
    }

    for (i = tamanho - 1; i >= 0; i--) {
         printf("%c", palavra[i]);
    }
    printf("\n");
}
'; for (i = 0; i < tamanho; i++) { repeticoes[palavra[i]]++; } for (i = 0; i < MAX_CHARS; i++) { if (repeticoes[i] > 0) { printf("%c = %d\n", (char) i, repeticoes[i]); } } for (i = tamanho - 1; i >= 0; i--) { printf("%c", palavra[i]); } printf("\n"); }

This repeticoes array is the hit counter. The type char occupies one byte, and therefore allows 256 different combinations. Then this will be the size of the array and there will be a position in the array for each possible value of char , each representing how many times that value of char appears in the word.

In the first for , the repeticoes[palavra[i]]++; statement works first with palavra[i] that will map the character of the word directly to one of the positions of the repeticoes array, which position will have its value increased. This loop will go through the whole typed word (or phrase) and counting the characters will mount the occurrence counter.

The second for only traverses the values of the occurrence counter, showing them on the screen. Zero positions are characters that do not exist in the word, which is why they are not shown.

The last for traverses the string palavra back-to-front and prints the characters one at a time, then showing the back-to-front string.

Also note that I put tamanho < TAM - 1 && in while . The reason for this is to avoid being able to type more characters than it fits in the palavra array. Using only tamanho < TAM is not sufficient because you still need space for tamanho < TAM - 1 , and therefore && is used. This is before %code% because the size has to be checked before any extra characters are read.

    
24.01.2018 / 04:08
1

You can use a simple function that can write to an integer vector the frequency distribution of each character of a given string ( ver histogram ), for example:

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

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

To display a string in reverse, you can use a function containing a for loop able to iterate in the string backwards, by printing character by character, see:

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

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

    printf("\n");
}

Putting it all together:

#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[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 = 0;

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

    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;
}

Output:

Um pequeno jabuti xereta viu dez cegonhas felizes.
[ ]: 7 *******
[a]: 3 ***
[e]: 8 ********
[i]: 3 ***
[n]: 2 **
[o]: 2 **
[s]: 2 **
[t]: 2 **
[u]: 3 ***
[z]: 2 **
.sezilef sahnogec zed uiv aterex itubaj oneuqep mU
    
24.01.2018 / 15:40
0

In the above case he would be picking up the number of times this character repeats, I think that's not what French K meant, but rather how many elements repeat themselves: if we use the word "mathematics" we have 3 elements that repeat the (a) and (t) in the above case the counter would return approximately 17, but should return 3, by what I understood from the question. I'm going to put a sample code based on the Gabriel Gomes code but I have not tested it.

int contador=0;
char encontrados[TAM];
for(int i=0;palavras[i]!='
int contador=0;
char encontrados[TAM];
for(int i=0;palavras[i]!='%pre%';i++){
    for(int j=0;palavras[j]!='%pre%';j++){
        for(int z=0;i<contador;z++){
            if(plavras[i]==palavras[j] && i!=j && palavras[i]==encontrados[z]){
                break;
            }
        }
        if(palavras[i]==palavras[j] && i!=j){
            contador++;
            break;
        }
    }
}
';i++){ for(int j=0;palavras[j]!='%pre%';j++){ for(int z=0;i<contador;z++){ if(plavras[i]==palavras[j] && i!=j && palavras[i]==encontrados[z]){ break; } } if(palavras[i]==palavras[j] && i!=j){ contador++; break; } } }
    
23.01.2018 / 22:17
-1

I believe it can be done this way ... Each element it compares with each one and, finding an equal, increments the counter.

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

#define TAM 100

main () {       
    char palavra[TAM];
    int contador = 0;

    printf("Digite uma palavra: \n" );
    scanf("%s", &palavra)

    for( i =0, palavra[i] != '
#include <stdio.h>
#include <stdlib.h>

#define TAM 100

main () {       
    char palavra[TAM];
    int contador = 0;

    printf("Digite uma palavra: \n" );
    scanf("%s", &palavra)

    for( i =0, palavra[i] != '%pre%'; i++ ){
        for( j =0, palavra[j] != '%pre%'; j++ ){
           if(palavra[i] == palavra[j])
             contador++;
         }
     }              

      printf ("%s = %d \n", &palavra, &contador);  
}
'; i++ ){ for( j =0, palavra[j] != '%pre%'; j++ ){ if(palavra[i] == palavra[j]) contador++; } } printf ("%s = %d \n", &palavra, &contador); }
    
23.01.2018 / 20:04