c++ - I get 4 when I use scanf_s but 1 when I use fgets()? -
#include "stdafx.h" #include <stdio.h> #include <string.h> void main() { char s[200]; int count = 0, i; printf("enter string\n"); //scanf_s("%[^\n]s", s, sizeof(s)); fgets(s, sizeof*(s), stdin); (i = 0;s[i] != '\0';i++) { if (s[i] == ' ') count++; } printf("number of words in given string are: %d\n", count + 1); getchar(); }
fgets(s, sizeof*(s), stdin);
-- wrong. should sizeof(s)
the *(s) character (first element of array), while s array. because single character of size 1, got 1.
Comments
Post a Comment