I've got this program:
#include <stdio.h>
void countCharacter(char str[], char *p, int Count)
{
p=str;
while(*p!='\0')
{
Count++;
p++;
}
printf("The string, %s, has %d characters", str, Count-1);
}
int main()
{
char str[100];
char *p;
int Count = 0;
printf("Enter any string: \n");
scanf("%c", str);
fgets(str, 100, stdin);
countCharacter(str, p, Count);
return 0;
}
*For this particular exercise I was not allowed to use the sizeof
operator or strlen()
function *
The code is supposed to count the number of characters in an array and give that as the output. For example, if I were to input test
I should get the following output:
Enter any string:
test
The string, test, has 4 characters.
However, I end up with this instead:
Enter any string:
test
The string test
has 4 characters
Any help on why this is would be greatly appreciated.