Home > Backend Development > C++ > C program to find string length

C program to find string length

WBOY
Release: 2023-09-11 19:53:02
forward
880 people have browsed it

C program to find string length

这个字符串实际上是一个由字符组成的一维数组,以一个null 字符'\0'结尾。因此,一个以null结尾的字符串包含组成字符串的字符,后面跟着一个null。

要找到字符串的长度,我们需要循环并计算循环中的所有字符,直到匹配到‘\0’字符为止。

例如

输入 −naman 

输出 − 字符串长度为5

解释 − 我们需要迭代字符串的每个索引,直到达到字符串的末尾,即‘\0’,即null字符。

示例

#include <stdio.h>
#include<string.h>
int main() {
   char string1[]={"naman"};
   int i=0, length;
   while(string1[i] !=&#39;\0&#39;) {
      i++;
   }
   length=i;
   printf(" string length is %d",length);
   return 0;
}
Copy after login

输出

string length is 5
Copy after login

The above is the detailed content of C program to find string length. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
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