Home >Backend Development >C#.Net Tutorial >How to find the length of a string in C language and output it
C language method to find the length of a string and output it: first use the strlen() function to calculate the length of the string and assign it to the variable len, the syntax is "len=strlen(string);"; then Use the printf() function to output the length, with the syntax "printf("%d\n",len);".
The operating environment of this tutorial: windows7 system, c99&&Dev-C version 5.11, Dell G3 computer.
In C language, you can use the strlen() function to find the length of a string.
The C language strlen function is used to find the length of a string (how many characters it contains).
strlen() function counts backwards from the beginning of the string until it encounters \0, and then returns the value of the timer. The final statistical string length does not include \0.
Header file: string.h
Syntax/Prototype:
size_t strlen(const char* str);
Parameter str represents the string of required length.
Return value: The length of the string str.
Example: strlen() function finds the length of the string input by the user and outputs it.
#include <stdio.h> #include <string.h> int main(){ char str[100] = { 0 }; size_t len; gets(str); len = strlen(str); printf("Length: %d\n", len); return 0; }
Run results:
Related recommendations: "C Language Video Tutorial"
The above is the detailed content of How to find the length of a string in C language and output it. For more information, please follow other related articles on the PHP Chinese website!