How do I remove the dot (.) from an array of characters?

1

I'm developing a program that reads a whole line, takes the last three values and replaces the comma by point, and then transforms this number to double. But I'm not able to develop a method if the number is, for example, 1**.**345,50 .

The point needs to be deleted in these cases first so that I can then replace the comma by point and subsequently convert to double.

Follow the code:

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

#define MAX_LINE 500

double strtodouble (char *array[MAX_LINE], int position){
    double num;
    char *ptr;

    ptr = strpbrk(array[position], ","); //retorna um ponteiro para a posição da string "," 
    if(ptr != NULL)   *ptr = '.';          //faz a substituição da ',' por '.'  
    num = atof(array[position]);       //converte string para float printf("num = %f", num);

    return num;

}

int main(void)
{

    char sFrase[]="1337-000/2018 01/10/2018 KI BARATO MERCEARIA DE DESCONT SECRETARIA DA CAMARA SECRETARIA DA CAMARA AÇÃO LEGISLATIVA Dispensa - Isento Compras e Serviços 2.343,50 343,40 343,30";
    int count = 0;
    char *p = strtok(sFrase, " ");
    char *array[500];
    //float valor = atof(*array);
    char *ptr;
    double num1, num2, num3;

    while (p)
    {
      array[count] = p;

      p = strtok (NULL, " ");

      count++;
    }

    //printf("%s\n", array[cont-1]);
    //printf("%s\n", array[cont-2]);
    //printf("%s\n", array[cont-3]);   

    //array[cont-3] = 343,50
    //ptr=array[cont-3][3]

    num1 = strtodouble(array, count-3);
    printf("\nESTE É O VALOR EMPENHADO --> %2.2f", num1 );

    num2 = strtodouble(array, count-2);
    printf("\nESTE É O VALOR LiQUIDADO --> %2.2f", num2 );

    num3 = strtodouble(array, count-1);
    printf("\nESTE É O VALOR PAGO --> %2.2f\n", num3 );

   return 0;
}
    
asked by anonymous 11.11.2018 / 20:46

1 answer

1

C does not have many functions already ready to use for certain types of things, this being one of them, to remove a caratere in an array of characters. Given that you also want to change the comma by point it is easier to use a loop / loop and do it all at once manually.

Example:

double strtodouble (char *array[MAX_LINE], int position){
    static char temp[MAX_LINE];
    int i, pos_temp = 0;
    for (i = 0; array[i] != '
double strtodouble (char *array[MAX_LINE], int position){
    static char temp[MAX_LINE];
    int i, pos_temp = 0;
    for (i = 0; array[i] != '%pre%'; ++i){
        if (array[position][i] == ','){ //se é virgula troca por ponto
            temp[pos_temp++] = '.';
        }
        else if (array[position][i] != '.'){ //se não for ponto coloca o caretere
            temp[pos_temp++] = array[position][i];
        }
    }
    temp[pos_temp] = '%pre%'; //terminador no novo

    return atof(temp); //atof agora direto no retorno
}
'; ++i){ if (array[position][i] == ','){ //se é virgula troca por ponto temp[pos_temp++] = '.'; } else if (array[position][i] != '.'){ //se não for ponto coloca o caretere temp[pos_temp++] = array[position][i]; } } temp[pos_temp] = '%pre%'; //terminador no novo return atof(temp); //atof agora direto no retorno }

See it working on Ideone

In the example above I chose to build a new array with all the characters that matter. As the point does not matter it is not copied to the new array and so ends up being indirectly deleted. This form is simpler than trying to remove a caratere in the middle as it would be necessary to pull all the characters to the right a house to the left.

    
11.11.2018 / 21:41