Divide vector according to delimiters and copy content to other strings in C

Asked

Viewed 1,031 times

1

I need the main string "vector" to be traversed to the delimiter '=' and copy the values before '=' to "second" and the values after '=' to "third". I’m sure there’s a smarter way to do this, but as a beginner, I’m having a hard time thinking about the right code for this requirement.

int tam = 256;

char vetor[tam];
char segundo[tam];
char terceiro[tam];
int i = 0;
int j = 0;

fgets(vetor, tam, stdin);

for (i = 0; i < strlen(vetor); i++){
    if (vetor[i] == '='){
        segundo[i] = vetor[i];
    } //continuar percorrendo a string e copiar valores após o delimitador '=' para o vetor "terceiro"
}

The intention is that when the expression is inserted for example: 9+8i*8-3i= it is possible to treat it in different vectors as follows: vector 1: 9 vector 2: +8i vector 3: *8 vector 4: -3i

If there are better ideas for calculating an expression with complex numbers, I am open to ideas =) Ps.1: I try to make the most of C’s native functions, trying to make the most of the external library functions.

2 answers

1


One possibility would be:

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

int main(){

    int tam = 256;
    char vetor[tam];
    char segundo[tam];
    char terceiro[tam];
    int i = 0;
    int j = 0;

    fgets(vetor, tam, stdin);

    for (i = 0; i < strlen(vetor); i++){
        if (vetor[i] != '='){
            segundo[i] = vetor[i];  // Copia apenas caracteres diferentes de '='
        }
        else{
            break;      // Para o loop caso seja encontrado um '='
        }
    }
    segundo[i] = '\0';  // Coloca o término de string (só pra garantir)

    // Aqui o '=' está na posição i, então temos que começar lendo de i+1

    for (j = i+1; j < strlen(vetor); j++){
        if (vetor[j] != '\0'){
            terceiro[j - i- 1] = vetor[j];
        }
        else{
            break;      // Para o loop caso a string acabe (chegue no '\0')
        }
    }
    terceiro[j - i - 1] = '\0';  // Coloca o término de string (só pra garantir)

    printf("segundo = %s\n", segundo);
    printf("terceiro = %s\n", terceiro);

}

Remembering that if the vector string does not have a '=' in the middle the program will not work properly.

  • Thanks for the answer! I did so and at the time of rotating, the vectors are divided properly as I expected even. The problem is that right after the results of the 2 resulting strings, some strange characters appear as "ýÿ " in the line just below the results. What could be?

  • Oh yes, instead of third[j] = ' 0'; It was supposed to have been third[j - i - 1] = ' 0'; I got the answer right.

0

How about using the function strtok() of the standard library string.h?

Follow (tested) example of a code similar to yours:

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

#define MAX_TAM   (256)

int main( void )
{
    FILE * pf = NULL;
    char * p = NULL;
    char vet[ MAX_TAM + 1 ] = {0};
    char segundo[ MAX_TAM + 1 ] = {0};
    char terceiro[ MAX_TAM + 1 ] = {0};

    pf = fopen( "input.txt", "r" );

    if(!pf)
    {
        fprintf( stderr, "Erro ao abrir arquivo de entrada.\n");
        return 1;
    }

    while( fgets( vet, MAX_TAM, pf ) )
    {
        vet[ strcspn( vet, "\n" ) ] = 0;

        p = strtok( vet, "=" );

        if( p )
        {
            strncpy( segundo, p, MAX_TAM );
            p = strtok( NULL, "=" );
            strncpy( terceiro, p, MAX_TAM );

            printf("segundo='%s' terceiro='%s'\n", segundo, terceiro );
        }
    }

    fclose(pf);

    return 0;
}

Entree:

fibo=1.6180
e=2.7182
pi=3.1415
sqrt2=1.4142
sqrt3=1.7320

Exit:

segundo='fibo' terceiro='1.6180'
segundo='e' terceiro='2.7182'
segundo='pi' terceiro='3.1415'
segundo='sqrt2' terceiro='1.4142'
segundo='sqrt3' terceiro='1.7320'

I hope I’ve helped!

  • Thanks for the reply @lacobus , but it wasn’t exactly what I was looking for. I got the solution to my problem by placing the close() function at the end of the main and adding one more condition to the if loops, as follows: if (vector[j] != ' 0' && vector[j] != ' n'){ I think what was happening is that when using fgets, The activation of the enter key was being computed as the vector value, changing the way the loop would be executed. Anyway, thanks for the disposition!

Browser other questions tagged

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