I was asked to fill in the blanks in order to make the program print the length of the string. The blank space is:
#include <stdio.h>
int main()
char str[101];
int strlen;
scanf( );
printf("%d", strlen);
return 0;
}
Now, I want to fill the blanks in the scanf()
area. I think neither the strlen
nor the loops can be used for this.
What should I code to print the length?
CodePudding user response:
It seems what you need is the following
scanf( "0s%n", str, &strlen );
Pay attention to that using the name strlen
for the variable that corresponds to the standard string function strlen
is a bad idea.
CodePudding user response:
Another possibility (though not quite exactly filling in your blanks) is:
#include <stdio.h>
int main()
{
char str[101];
int len;
printf("enter a string: "); fflush(stdout);
if(scanf("0s", str) == 1) {
printf("the length of the string \"");
len = printf("%s", str);
printf("\" is %d\n", len);
}
}