How to delete a repeated character in C?

5

I am filling out a form with validations and there is a field that asks me for an average, and one of the rules to validate it is that it only contains one decimal point.

Example:

If the user types 2..2, ask the average question again.

Does anyone have an idea of how to do it? Thank you. : D

I have this code, but it eliminates all the repeated ones, I just want you to delete the point '.'

#include <stdio.h> 
#include <string.h>  
int main() 
{ 
    char resultado[27]=""; 
    char palabra[50];
    int i=0,j=0; 
    printf("Ingresa una frase!\n");scanf("%s",palabra); 
    while(palabra[i]) 
    { 
        if(!strchr(resultado,palabra[i])) 
            resultado[j++]=palabra[i]; 
        i++; 
    } 
    resultado[j]='
#include <stdio.h> 
#include <string.h>  
int main() 
{ 
    char resultado[27]=""; 
    char palabra[50];
    int i=0,j=0; 
    printf("Ingresa una frase!\n");scanf("%s",palabra); 
    while(palabra[i]) 
    { 
        if(!strchr(resultado,palabra[i])) 
            resultado[j++]=palabra[i]; 
        i++; 
    } 
    resultado[j]='%pre%'; 
    printf("\nEl resultado seria!\n%s",resultado); 

    getchar(); 
    return(0);
}
'; printf("\nEl resultado seria!\n%s",resultado); getchar(); return(0); }
    
asked by Jonathan Ortz 12.04.2016 в 20:23
source

2 answers

3

The solution of Alvaro Montoro funcona; however, it is a very I propose to use the library <regex> (as Alvaro has already suggested), which allows operations to be done in a few lines:

bool punto_repetido(const std::string &entrada)
{
    std::regex puntos{R"(\d*\.{2,}\d*)"};
    return std::regex_match(entrada, puntos);
}

The punto_repetido function will indicate if the user has typed a number with two points instead of one; you can correct the entry with this other function:

std::string corrige(const std::string &entrada)
{
    std::regex puntos{R"(\.{2,})"};
    return std::regex_replace(entrada, puntos, ".");
}

You can see the working code here .

Keep in mind that you only value entries that look like a number (you will not rate 2.2.2 , nor aaadfa...a for example).

    
answered by 13.04.2016 в 11:15
0

The code you share removes all the repeated characters in the string, but you only want to delete some characters (the points) after the first point.

For this you could follow the following algorithm:

  • Go through the read string
  • If the character is not a point
  • Copy the character to the result
  • If the character is a point
  • If it is the first point that is found
  • Copy the point to the result
  • The code would be like this:

    #include <stdio.h> 
    #include <string.h>
    
    int main() 
    { 
        char resultado[27]=""; 
        char palabra[50];
        int i=0,j=0; 
        int punto = 0; // variable centinela para comprobar el número de puntos
    
        printf("Ingresa una frase!\n");
        scanf("%s",palabra);
    
        // atraviesa la palabra leida
        for (i = 0; i < strlen(palabra); i++) {
            // si la palabra es un punto
            if (palabra[i] == '.') {
                // y es el primer punto que se encuentra
                if (punto == 0) {
                    // activa el centinela y añade el punto al resultado
                    punto = 1;
                    resultado[j++] = '.';
                }
            // si no es un punto, añade el carácter/número al resultado
            } else {
                resultado[j++] = palabra[i];
            }
        }
    
        resultado[j] = '
    #include <stdio.h> 
    #include <string.h>  
    int main() 
    { 
        char resultado[50] = ""; 
        char palabra[50];
        int i=0,j=0; 
        char *subcadena;
    
        printf("Ingresa una frase!\n");scanf("%s",palabra);
    
        // si se encuentra un punto en la palabra
        if (subcadena = strchr(palabra, '.')) {
            // actualiza los valores necesitados
            i = subcadena - palabra + 1;
            memcpy( resultado, palabra, i);
            j = strlen(resultado);
        }
    
        // atraviesa la palabra
        while(palabra[i]) 
        {
            // solo insertando los caracteres que no son puntos 
            if(palabra[i] != '.') 
                resultado[j++]=palabra[i]; 
            i++; 
        } 
        resultado[j]='
    #include <stdio.h> 
    #include <string.h>
    
    int main() 
    { 
        char resultado[27]=""; 
        char palabra[50];
        int i=0,j=0; 
        int punto = 0; // variable centinela para comprobar el número de puntos
    
        printf("Ingresa una frase!\n");
        scanf("%s",palabra);
    
        // atraviesa la palabra leida
        for (i = 0; i < strlen(palabra); i++) {
            // si la palabra es un punto
            if (palabra[i] == '.') {
                // y es el primer punto que se encuentra
                if (punto == 0) {
                    // activa el centinela y añade el punto al resultado
                    punto = 1;
                    resultado[j++] = '.';
                }
            // si no es un punto, añade el carácter/número al resultado
            } else {
                resultado[j++] = palabra[i];
            }
        }
    
        resultado[j] = '
    #include <stdio.h> 
    #include <string.h>  
    int main() 
    { 
        char resultado[50] = ""; 
        char palabra[50];
        int i=0,j=0; 
        char *subcadena;
    
        printf("Ingresa una frase!\n");scanf("%s",palabra);
    
        // si se encuentra un punto en la palabra
        if (subcadena = strchr(palabra, '.')) {
            // actualiza los valores necesitados
            i = subcadena - palabra + 1;
            memcpy( resultado, palabra, i);
            j = strlen(resultado);
        }
    
        // atraviesa la palabra
        while(palabra[i]) 
        {
            // solo insertando los caracteres que no son puntos 
            if(palabra[i] != '.') 
                resultado[j++]=palabra[i]; 
            i++; 
        } 
        resultado[j]='%pre%'; 
    
        printf("\nEl resultado seria!\n%s",resultado); 
    
        getchar(); 
        return(0);
    }
    
    '; printf("\nEl resultado seria!\n%s",resultado); getchar(); return(0); }
    '; printf("\nEl resultado seria!\n%s",resultado); getchar(); return(0); }
    '; printf("\nEl resultado seria!\n%s",resultado); getchar(); return(0); }

    A slightly different alternative that looks more like the code you showed:

  • Initializes the variables i and j to 0.
  • Initializes variable resultado to "".
  • Use strchr to find the first point in the string read
  • If a point was found
  • Update the value of i to the position of the point in the string
  • Update the value of resultado to the substring to the point (you can use memcpy for it)
  • Update the value of j to the length of resultado
  • Traverse the string read from position i .
  • If the character is not a point, add it to the result
  • If you look at steps 1, 2 and 5, they are (almost) the same as what you already have, and you would only have to add steps 3 and 4, which are to update the variables used in step 5 and following.

    The code would be like this:

    %pre%     
    answered by 13.04.2016 в 04:07