I have a question regarding the reading of strings in C. I wrote this code and they corrected me the part where the text string is read. I previously had this written:
scanf("%10000[^\n]\n",texto);
and I think I did not really understand the entry of strings in C. If someone could explain to me what is the difference of the following two entries:
scanf("%10000[^\n]\n",texto);
and scanf("%10000[^\n]s", texto); getchar();
and also what is the goal of using getchar()
. If someone could answer these questions, I would greatly appreciate it. Greetings
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
char texto[10001];
char cadena[101];
// ENTRADAS.
scanf("%10000[^\n]s", texto);
getchar();
scanf("%100[^\n]s",cadena);
//-----------------------Proceso-------------------------
char *puntero = texto;
unsigned short longitud = strlen(cadena);
char auxiliar[10001];
while ( (puntero = strstr(texto,cadena)) != NULL)
{
strcpy(auxiliar,"scanf("%10000[^\n]\n",texto);
");
strncat(auxiliar, texto, puntero - texto);
puntero = puntero + longitud;
strcat(auxiliar,puntero);
strcpy(texto, "#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
char texto[10001];
char cadena[101];
// ENTRADAS.
scanf("%10000[^\n]s", texto);
getchar();
scanf("%100[^\n]s",cadena);
//-----------------------Proceso-------------------------
char *puntero = texto;
unsigned short longitud = strlen(cadena);
char auxiliar[10001];
while ( (puntero = strstr(texto,cadena)) != NULL)
{
strcpy(auxiliar,"%pre%");
strncat(auxiliar, texto, puntero - texto);
puntero = puntero + longitud;
strcat(auxiliar,puntero);
strcpy(texto, "%pre%");
strcpy(texto,auxiliar);
}
printf("%s",texto);
return 0;
}
");
strcpy(texto,auxiliar);
}
printf("%s",texto);
return 0;
}