Reputation: 137
i've written a simple program, where the program takes and imput and prints out the characters in sequence and counts them
char name[100];
int counter=0
printf("Enter name and surname:");
scanf("%s",&name);
for(int i=0; i<(strlen(name)); i++){
printf("%c \n",name[i]);
counter++;
printf("%d",counter)
for some reason , when i imput John Snow, it prints out only
J
o
h
n
4
desired output
J
o
h
n
S
n
o
w
10
any help please? within reasonable skill level :D if posible no malloc() free() and memory things, its supposed to be quite easy :D
Upvotes: 0
Views: 767
Reputation: 311088
For starters you have to write
scanf("%s",name);
instead of
scanf("%s",&name);
Secondly when the format string "%s"
is used the function stops to enter characters as soon as a white space character is encountered.
You should write instead
scanf( " %99[^\n]", name );
Another approach is to use the function fgets
fgets( name, sizeof( name ), stdin );
The function can append the new line character '\n'
to the entered string. To remove it you can write
name[ strcspn( name, "\n" ) ] = '\0';
Upvotes: 1