'What you think about printing the string length without strlen() or loops in C? Is that possible? [closed]
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?
Solution 1:[1]
It seems what you need is the following
scanf( "%100s%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.
Solution 2:[2]
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("%100s", str) == 1) {
printf("the length of the string \"");
len = printf("%s", str);
printf("\" is %d\n", len);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Steve Summit |
