How to remove the dot (.) from a character array?

Asked

Viewed 72 times

1

I’m developing a program that reads an entire line takes the last three values and replaces the comma by dot and then turns 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 then turn into double.

Follows 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;
}
  • But what is not done? Or in other words, what remains to be done? There is some error ?

  • Unless I can eliminate the point of the number 2.343,50.

  • Within the strtodouble function I was able to replace the dot with 0. Now I can’t think of how to remove this 0. I want it to remove the dot and continue reading the number. The number is like this: 2.343,50 I want it to stay 2343,50 .... ptr = strpbrk(array[position], "."); //returns a pointer to the position of the string "." if(ptr != NULL)cheg *ptr = '0'; //does the substitution of '.' for '0' }

1 answer

1


C does not have many ready-to-use functions for certain types of things, this being one of them, to remove a character in an array of characters. Taking into account that you also want to exchange the comma for a point it is easier to use a loop/cycle 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] != '\0'; ++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] = '\0'; //terminador no novo

    return atof(temp); //atof agora direto no retorno
}

Watch it work on Ideone

In the above example 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 deleted indirectly. This form is simpler than trying to remove a character in the middle, as it would be necessary to pull all characters on the right a house to the left.

  • Perfect!! I wanted to find a way to do the two things separately, first remove the point and then remove the comma. Thank you for the resolution! Spun perfectly!

Browser other questions tagged

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