Not to show repeated letters

-1

I want to go through the word and iterate over each of its letters. If it detects that it has already been printed, and does not show the letter (with the help of an auxiliary arrangement), my problem is that I do not know how to carry it out. -

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

int main()
{

  int conta, conta_car, conta_max, max;
  char car, cadena[1025];

  printf("\n\nIntroduce la cadena que quieres analizar (1024 caracteres max): ");

  fgets(cadena, 1024, stdin);

  cadena[strlen(cadena)-1]=0;

  printf("\n\nIntroduce el caracter que quieres buscar: ");

  scanf("%c", &car);

  conta=0;
  max=0;

  while(cadena[conta]!=0)
  {

    while(cadena[conta]!=0 && cadena[conta]!=car)
      conta++;

    conta_car=0;

    while(cadena[conta]!=0 && cadena[conta]==car)
    {
      conta_car++;

      if(conta_car>max)
      {
        max=conta_car;
        conta_max=1;
      }
      else if(conta_car==max)
        conta_max++;

      conta++;                             
    }
  }

  if(max>0)
    printf("\n\nSe han encontrado %d '%c' consecutivos %d veces.\n\n", max, car, conta_max);
  else
    printf("\n\nNO se ha encontrado el caracter '%c' en la cadena.\n\n", car);

  system("pause");

  return 0;
}
    
asked by Maria Diaz 25.06.2017 в 20:12
source

3 answers

1

According to what I have understood of the title, you want to make a program that according to "the letters" that are introduced, that is, the frace introduced, count the letters that are repeated throughout the entire sentence.

I would leave with a safe alternative, something like the following:

#include <stdio.h>

/* Usaremos como longitud maxima 1024 caracteres. */
#define MAX_LEN (1024)

int main(void) {
    char b_frase[MAX_LEN] = {0};      /* buffer de entrada */
    int  b_letras[30] = {0};          /* Buffer para las letras. */
    int c = 0, ind = 0, i;            /* Otras variables. */

    /* Pedimos una frase :) */
    printf("Introduzca una frase (Enter para finalizar lectura): ");
    while ((ind < MAX_LEN) && ((c = fgetc(stdin)) != '\n')) { 
        b_frase[ind++] = c; /* Asignamos el elemento a la frase. */
        /* Nos encargamos de ver si es una letra... */
        if (c >= 'a' && c <= 'z')
            ++b_letras[c - 'a'];
        else if (c >= 'A' && c <= 'Z')
            ++b_letras[c - 'A'];
        /* No hacemos nada de lo contrario. */
    }
    b_frase[ind] = 0; /* Agrega el caracter nulo. */

    /* Imprimimos todo :) */
    printf("En la frase:\n%s\n\nLas Siguientes letras se repiten:\n\n", 
           b_frase);

    i = 'a';
    while (i <= 'z') {
        if (!b_letras[i - 'a']) { 
            ++i; continue; 
        }
        printf("%c se repite '%d' veces.\n", i, b_letras[i - 'a']), ++i;
    }
    return 0;
}

Using the following program, I tried to run it with the following per entry:

hola munda

I get the following result:

En la frase:
hola munda

Las Siguientes letras se repiten:
a se repite '2' veces.
d se repite '1' veces.
h se repite '1' veces.
l se repite '1' veces.
m se repite '1' veces.
n se repite '1' veces.
o se repite '1' veces.
u se repite '1' veces.

It is worth mentioning that this implementation ignores any character different from a capital or lowercase letter and takes all the letters equally, tested with the following entry:

AaAbCoOopP

I get by exit:

En la frase:
AaAbCoOopP

Las Siguientes letras se repiten:

a se repite '3' veces.
b se repite '1' veces.
c se repite '1' veces.
o se repite '3' veces.
p se repite '2' veces.

Greetings:)

    
answered by 27.06.2017 в 17:04
1

Looking at your code to understand the goal you want to achieve, here I am passing a solution that I believe corresponds to your description. Keep in mind that I use the __purge() function of the standard GNU library to clean the input buffer stdin , so if it gives you problems in another operating system, simply remove the call to this function.

Any other questions you may have regarding the code, do not hesitate to comment on it.

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

#define TAM_TEXT_MAX  1024

typedef unsigned short Ushort;
typedef enum {no_encontrada = 0, primera_vez = 1, mas_veces = 2} Aparicion;

int main(void)
{
  char texto[TAM_TEXT_MAX], letra;
  Ushort  longitudTexto, 
          repeticiones = 0,  
          consecutivas = 0,
          totalConsecutivas = 0,
          maxConsecutivas = 0,
          gruposConsecutivas = 0,
          gruposMaxConsecutivas = 0;
  Aparicion aparicion = no_encontrada;

  printf("Introduce el texto: ");
  __fpurge(stdin);
  fgets(texto, TAM_TEXT_MAX, stdin);
  longitudTexto = strlen(texto);

  printf("Introduce la letra: ");
  __fpurge(stdin);
  scanf(" %c", &letra);   // Hay un espacio en formato para ignorar caracteres en blanco.

  for (Ushort i = 0; i <= longitudTexto; ++i)
  {
    if (i < longitudTexto && texto[i] == letra)
    {
      ++repeticiones;
      switch (aparicion)
      {
        case no_encontrada:
          aparicion = primera_vez;
          break;
        case primera_vez:
          consecutivas += 2;
          aparicion = mas_veces;
          break;
        case mas_veces:
          ++consecutivas;
          break;
      }
    }
    else
    {
      switch (aparicion)
      {
        case mas_veces:
          if (consecutivas == maxConsecutivas)
            ++gruposMaxConsecutivas;
          else if (consecutivas > maxConsecutivas)
          {
            maxConsecutivas = consecutivas;
            gruposMaxConsecutivas = 1;
          }
          ++gruposConsecutivas;
          totalConsecutivas += consecutivas;
          consecutivas = 0;

        case primera_vez:
          aparicion = no_encontrada;

        case no_encontrada:
          break;
      }
    }
  }

  printf("Los datos de la letra «%c» en el texto son:\n"
         "%-40s %05hu\n"
         "%-40s %05hu\n"
         "%-42s %05hu\n"
         "%-42s %05hu\n"
         "%-40s %05hu\n", letra,
         "Apariciones totales:", repeticiones,
         "Consecutivas en total:", totalConsecutivas,
         "Máximo nº de consecutivas:", maxConsecutivas,
         "Grupos de máximo nº de consecutivas:", gruposMaxConsecutivas,
         "Grupos totales de consecutivas:", gruposConsecutivas);

  return 0;
}
    
answered by 29.06.2017 в 08:11
0

I would implement it in another way. Instead of using fgets() to request the string, I would use gets() which is easier. Once you have the string and the letter to find your own would be to walk it with a bulce for and read it from the initial position (i = 0) to another variable that you declare using the strlen() that directly gives you the length of the chain so you know perfectly between what values of your chain you are moving. Once done that is simply check position by position to see if the letters are equal and if they are to check the next position to see if they are consecutive or not. This implemented in code would be something like this:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 1024


int main() {
char cadena[N]; //Creamos el array que tendra un maximo de N elementos donde N = 1024
char letra;
int i, repeticiones = 0, consecutivas = 0, longitud;

printf("\nIntroduce la cadena a analizar: ");
fflush(stdin);
gets(cadena);

longitud = strlen(cadena); //Determinamos cuantas de esas 1024 posiciones se han rellenado

printf("\nIntroduce la letra a buscar: ");
fflush(stdin);
scanf("%c",&letra);

for(i = 0; i < longitud; i++)
{

    if(cadena[i] == letra) //Comprobamos si la posicion es igual a la letra y aumentamos el contador en caso de que lo sea
    {
        repeticiones = repeticiones + 1;

        if (cadena[i + 1] == letra) //Si la posicion es igual a la letra comprobamos si la siguiente letra es igual
        {
            consecutivas = consecutivas + 1;
        }
    }

}

printf("\nEl numero de repeticiones de la letra %c es: %i de las cuales son consecutivas %i\n\n\n",letra,repeticiones,consecutivas);

return 0; 
}

You can also do it in another way but it is the fastest way that has occurred to me. I hope it serves you

    
answered by 27.06.2017 в 15:58