Because when I use fgets(string,5,stdin) , and I insert only two AB characters, the command strlen(string) returns the value of 3
#include<stdio.h>
#include<string.h>
int main() {
char nombre[20]="";
printf("Ingresa Nombre: ");
fgets(nombre,5,stdin);
printf("Cadena es : %s\n",nombre);
printf("Longitud es : %d\n",strlen(nombre));
printf("nombre[0]= %c\n",nombre[0]);
printf("nombre[1]= %c\n",nombre[1]);
printf("nombre[2]= %c\n",nombre[2]);
printf("nombre[3]= %c\n",nombre[3]);
printf("nombre[4]= %c\n",nombre[4]);
return 0;
}
When we run it we have
Ingresa Nombre: AB
Cadena es : AB
Longitud es : 3
nombre[0]= A
nombre[1]= B
nombre[2]=
nombre[3]=
nombre[4]=
Obs I see an ERROR Because strlen(string) returns me 3 , if I enter : AB it seems that fgets fills with an ENTER . I need strlen(string) to return the correct value, that is, 2. How should I do?