#include <stdio.h>
#include <stdlib.h>
int main()
{
char string[100];
int count=0;
char *pString = string;
printf("Enter a string: \n");
scanf("%s",string);
while(*pString != '\0')
count++;
printf("String is %d characters long",count);
return 0;
}
Heres my code. I want to write a simple function to count the number of characters in a string array. The problem is it gets stuck after I enter the string. And the console just freezes. It doesn't release the output.
scanf("%s",string);1) always check the returned value (not the parameter values) to assure the operation was successful. Note: the scanf() family of functions returns the number of successful 'format conversion specifiers' (in this case any returned value other than 1 indicates an error. 2) when using '%s' and/or '%[...]' always include a MAX CHARACTERS modifier that is 1 less than the length of the input buffer. This avoids any possibility of a buffer overflow and the attendant undefined behavior