Save string of characters in C

1

How could I read a character string and store it in a variable? The variable must not have a fixed size. It's costing me a little. Thanks!

I'm sorry I have hardly any code, everything I try is wrong. I'm trying to do it with a malloc but there's no way: (

    
asked by Moltkerrimo 03.11.2016 в 19:46
source

3 answers

6
  

How could I read a character string and store it in a variable?

To read a value from the keyboard you can use scanf . To this function you must indicate the format of the data you want to read and the address of the variable in which you want to read them, let's see it in an example:

int numero = 0;
scanf("%d", &numero);

The "%d" indicates that you intend to read a number, if you want to read a string of characters you should use "%s" :

char cadena_de_caracteres[100];
scanf("%s", cadena_de_caracteres);

Here we do not use the operator address-of ( the et & ) because the name of the fix equivalent to the address of the first element of it. Keep in mind that reading "%s" adds the end character of control : the null character .

  

The variable must not have a fixed size.

If it is not a fixed size: it is variable size. To do this you must first find out the size of the string you want to read and then read the string:

int longitud_de_la_cadena = 0;
printf("Longitud de la cadena: ");
scanf("%d", &longitud_de_la_cadena);

char *cadena = (char *)malloc(longitud_de_la_cadena + 1);
printf("Cadena: ");
scanf("%s", cadena);

printf("\"%s\" es la cadena de %d caracteres introducida.\n", cadena, longitud_de_la_cadena);

The previous code by entering the values 6 and patata shows:

Longitud de la cadena: 6
Cadena: patata
"patata" es la cadena de 6 caracteres introducida.

Here we use malloc to accommodate the size we need for our variable length string, not we must forget to release the requested memory before finishing the program using the function free :

free(cadena);
    
answered by 04.11.2016 / 12:43
source
2

It could be this following example as a help to your question:

  

Perform a program that allows a user to enter by keyboard   several data that will be stored in an array (array) of strings   (strings). The program at the beginning should ask for the quantity and size of   the data to enter.

#include <stdio.h>

int main()
{
    int i,cant,tam;

    printf("Ingresar una cantidad de datos: ");
    scanf("%d",&cant);
    printf("Ingresar el tamanio: ");
    scanf("%d",&tam);

    char *arreglo[cant];
    char* dato;

    for(i=0; i<cant; i++){
        printf("Ingresar dato %i: ",i);
        dato = (char*) malloc(tam);
        scanf("%s",dato);
        arreglo[i] = dato;
    }

    printf("\nLos datos ingresados son: \n");
    for(i=0; i<cant; i++){
        printf("=>%s\n",arreglo[i]);
    }
    return 0;
}    

I hope this can help you, regards.

    
answered by 03.11.2016 в 21:11
1

You have been given several examples in which you have to indicate the size ... here is a different approach:

int main()
{
  printf("Introduce una cadena:\n");

  char* ptr = 0;
  int ptrSize = 0;

  while( 1 )
  {
    ptr = (char*)realloc(ptr,ptrSize + 1);
    int c = getchar();
    if( c == EOF )
    {
      ptr[ptrSize] = 0;
      break;
    }
    ptr[ptrSize] = (char)c;
    ptrSize++;
  }

  printf("Cadena introducida:\n%s",ptr);
  free(ptr);
}

In this case, the memory where the string is stored is resized as characters are entered ... so that it is not necessary to indicate previously how many characters are to be entered

    
answered by 26.01.2018 в 07:42