How to find the length of a string in C language and output it

青灯夜游
Release: 2023-01-05 16:13:54
Original
18560 people have browsed 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);".

How to find the length of a string in C language and output it

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);
Copy after login
  • 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;
}
Copy after login

Run results:

How to find the length of a string in C language and output it

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!