Problem with scanf en strings

0
char palabra [20];

char letra;

printf("Ejercicio para eliminar un caracter de una palabra\n");
fflush(stdout);

printf("Introduzca la palabra deseada:\n");
fflush(stdout);
gets(palabra);
//scanf("%s",palabra);



printf("Introduzca el caracter deseado:\n");
fflush(stdout);
scanf("%c",&letra);


quitacaracter(palabra,letra);

return 0;

I was trying to make a code that read a string and a character and return that string without the entered character, well when reading the string with gets everything perfect but when I read it with scanf ("% s", word) it jumps reading the character and can not solve it.

    
asked by Jose Manuel 02.03.2018 в 16:49
source

1 answer

0

To avoid this problem add a space to the reading of the character:

scanf(" %c",&letra);
//     ^

In this way scanf will discard the line break without problems ... if there is one. If there is no line break it will also work.

    
answered by 02.03.2018 в 22:11