Function that displays the characters of a repeating string

Asked

Viewed 148 times

0

Example: In the string "programming" the characters 'a', 'o' and 'r' must be returned. I did so but did not return what is requested.

#include <stdio.h>
#include <stdlib.h>
#define DIM 30

void repete(char *str) {

    int rep = 0;
    char repetidos[DIM];

    //COMPARANDO OS CARACTERES PARA CHECAR A REPETIÇÃO
    for (int i = 0; str[i] != '\0'; i++) {
        for (int j = 1; str[j] != '\0'; j++) {
            if(str[i] == str[j]) {
            repetidos[i] = str[i]; //ADICIONANDO OS CARACTERES REPETIDOS NO VETOR
            rep++;
            }
        }
    }

    printf("Caracteres que se repetem: ");
    for (int k = 0; k < rep ; k++) {
        printf("%c ", repetidos[k]);
    }
} 

int main(void) {

    char word[DIM];
    char *ptr;
    ptr = word;

    printf("Informe uma palavra: ");
    gets(word);
    repete(ptr);

    return 0;
}

2 answers

0

No need to compare from the beginning, just compare the characters in front and check if it is not yet in the repeat list.

#include <stdio.h>
#include <stdlib.h>
#define DIM 30

void repete(char *str) {

    int rep = 0;
    char repetidos[DIM]="";

    //COMPARANDO OS CARACTERES PARA CHECAR A REPETIÇÃO
    for (int i = 0; str[i] != '\0'; i++) {
        for (int j = i+1; str[j] != '\0'; j++) {
            if(str[i] == str[j]) {
                if (repetidos[rep] != str[i]) {
                    repetidos[rep] = str[i]; //ADICIONANDO OS CARACTERES REPETIDOS NO VETOR
                    rep++;
                    repetidos[rep] = '\0';
                }
            }
        }
    }

    printf("Caracteres que se repetem: ");
    for (int k = 0; k < rep ; k++) {
        printf("%c ", repetidos[k]);
    }
} 

int main(void) {

    char word[DIM];
    char *ptr;
    ptr = word;

    printf("Informe uma palavra: ");
    gets(word);
    repete(ptr);

    return 0;
}

Another possibility would be to sort all characters and count any repetitions.

0


You can use the function strchr() of the standard library string.h to simplify your algorithm.

The function strchr() traverse a string in search of the specified character and return NULL if the character is not contained within this string.

See just how your program could look:

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

#define DIM 30

void repete( char *rep, const char *str ) {
    while(*str) {
        if(strchr(str + 1, *str))
            if(!strchr(rep, *str))
                strncat(rep, str, 1);
        str++;
    }
}

int main(void) {
    char palavra[DIM] = "";
    char repetidos[DIM] = "";

    printf("Informe uma palavra: ");
    gets(palavra);

    repete(repetidos, palavra);

    printf("Letras repetidas: %s\n", repetidos);

    return 0;
}

See working on Repl.it

If you want to calculate the frequency at which each character appears in a given string, you can use a calculation algorithm of histograma, look at you:

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

#define DIM      30
#define HIST_DIM 256


void calcular( int *hist, const char*str) {
    memset( hist, 0, HIST_DIM * sizeof(int));
    while(*str)
        hist[(unsigned char) *(str++)]++;
}


int main(void) {
    unsigned char c;
    int histograma[HIST_DIM];
    char palavra[DIM] = "";

    printf("Informe uma palavra: ");
    gets(palavra);

    calcular( histograma, palavra );

    for( c = 'a'; c <= 'z'; c++ )
        printf("%c = %d\n", c, histograma[c]);

    return 0;
}

Exit:

Informe uma palavra: programacao
a = 3
b = 0
c = 1
d = 0
e = 0
f = 0
g = 1
h = 0
i = 0
j = 0
k = 0
l = 0
m = 1
n = 0
o = 2
p = 1
q = 0
r = 2
s = 0
t = 0
u = 0
v = 0
w = 0
x = 0
y = 0
z = 0

See working on Repl.it

Browser other questions tagged

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