#include <stdio.h> #include <stdlib.h> #include <string.h>
#define LONG_CADENA 80 #define CHAR_A_ESCAPAR '\''
int main(void) { char entrada[LONG_CADENA], *salida, *aux; int i, j; printf("Entrada: "); fflush(stdout); if (fgets(entrada, LONG_CADENA, stdin) == NULL){ fprintf(stderr, "Error al tratar de leer stdin!\n"); return EXIT_FAILURE; } if ((aux = strchr(entrada, '\n')) != NULL) *aux = '\0'; if ((salida = malloc(strlen(entrada) * 2 + 1)) == NULL){ fprintf(stderr, "Error con malloc!\n"); return EXIT_FAILURE; } for (i = 0, j = 0; entrada[i] != '\0'; i++){ if (entrada[i] == CHAR_A_ESCAPAR) salida[j++] = '\\'; salida[j++] = entrada[i]; } salida[j] = '\0'; printf("Salida: %s\n", salida); free(salida); return EXIT_SUCCESS; } |