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

C program to find string length

WBOY
WBOYforward
2023-09-11 19:53:02880browse

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;
}

输出

string length is 5

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete