The pointer does not return the full value of the string

1

Hello, I have problems with this code that must convert an integer to binary and keep the zeros and ones in a pointer of type char. However after the execution the pointer only returns a 1 and not all the values of the string

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

/*
 * 
 */
int * permutaInt;

char * permuta(char * str, int x, int base) {
    int division = x;
    int posicion = 0;
    int *copia;

    while (division != 0) {

        str[posicion] = 48 + (division % base);
        division = division / base;
        posicion++;
    }

    posicion = 0;

    while (str[posicion] != '
#include <stdio.h>
#include <stdlib.h>

/*
 * 
 */
int * permutaInt;

char * permuta(char * str, int x, int base) {
    int division = x;
    int posicion = 0;
    int *copia;

    while (division != 0) {

        str[posicion] = 48 + (division % base);
        division = division / base;
        posicion++;
    }

    posicion = 0;

    while (str[posicion] != '%pre%') {
        posicion++;
    }

    copia = (char*) malloc(posicion * sizeof (char));

    posicion = posicion - 1;
    int contador = 0;

    while (posicion != -1) {
        copia[contador] = str[posicion];
        posicion--;
        contador++;
    }
    copia[contador++]='%pre%';
    contador=0;

    return copia;

}

int main() {

    char *str;
    char *recibido;
    str =(char*)malloc(100*sizeof(char));
    int x = 0;
    int base = 2;
    int contador=0;

    printf("Ingresa tu numero \n");
    scanf("%d", & x);



    recibido=permuta(str, x, base);
    printf("%s",recibido);

    return (EXIT_SUCCESS);
}
') { posicion++; } copia = (char*) malloc(posicion * sizeof (char)); posicion = posicion - 1; int contador = 0; while (posicion != -1) { copia[contador] = str[posicion]; posicion--; contador++; } copia[contador++]='%pre%'; contador=0; return copia; } int main() { char *str; char *recibido; str =(char*)malloc(100*sizeof(char)); int x = 0; int base = 2; int contador=0; printf("Ingresa tu numero \n"); scanf("%d", & x); recibido=permuta(str, x, base); printf("%s",recibido); return (EXIT_SUCCESS); }
    
asked by JAMG54 11.11.2017 в 19:37
source

1 answer

1

I suggest that, instead of 1 function that does everything , use several, following the principle of sole responsibility ; It will be easier for you to develop the code as well as the possible one. Not to mention being able to use each part in future codes ...

  • char *int2bin( int x, int base ) : converts a number to the indicated base, returning a text string that is reserved within the function itself.

It's a copy / paste of your code: -)

One possible improvement (he's screaming for it) is that, instead of a fixed size for the output chain, reserve the right amount to use.

  • char *strrev( const char *orig, char *dst ) : invert a text string. orig is the original string, and dst (which can be NULL ) is the result string. NO you can use the same string as source and destination, eye;)

If dst == NULL , the function internally reserves a string of the necessary fair space.

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

char *int2bin( int division, int base ) {
  char *str = malloc( 50 );
  int posicion = 0;

  while( division != 0 ) {
    str[posicion] = 48 + ( division % base );
    division = division / base;
    posicion++;
  }

  str[posicion] = 0;

  return str;
}

char *strrev( const char *or, char *dst ) {
  size_t orsize = strlen( or );

  if( !dst ) dst = malloc( orsize + 1 );
  char *target = dst;

  char *idx = or + orsize - 1;

  while( idx >= or ) {
    *target = *idx;
    ++target;
    --idx;
  }

  *target = '
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char *int2bin( int division, int base ) {
  char *str = malloc( 50 );
  int posicion = 0;

  while( division != 0 ) {
    str[posicion] = 48 + ( division % base );
    division = division / base;
    posicion++;
  }

  str[posicion] = 0;

  return str;
}

char *strrev( const char *or, char *dst ) {
  size_t orsize = strlen( or );

  if( !dst ) dst = malloc( orsize + 1 );
  char *target = dst;

  char *idx = or + orsize - 1;

  while( idx >= or ) {
    *target = *idx;
    ++target;
    --idx;
  }

  *target = '%pre%';

  return dst;
}

int main( void ) {
  int x;

  printf( "Introduzca un número entero positivo: " );
  scanf( "%d", &x );

  char *bin = int2bin( x, 2 );
  char *rev = strrev( bin, NULL );

  printf( "Entero: %d, binario: %s, invertido: %s\n", x, bin, rev );

  return 0;
}
'; return dst; } int main( void ) { int x; printf( "Introduzca un número entero positivo: " ); scanf( "%d", &x ); char *bin = int2bin( x, 2 ); char *rev = strrev( bin, NULL ); printf( "Entero: %d, binario: %s, invertido: %s\n", x, bin, rev ); return 0; }
    
answered by 12.11.2017 в 18:05