C 程序 查找字符串的长度

来源:这里教程网 时间:2026-02-16 13:03:41 作者:

在下面的 C 程序中,我们计算给定字符串中的字符数,并在控制台上显示其长度。在执行该程序时,将要求用户输入字符串,然后程序将对字符进行计数并输出字符串的长度。

C 程序 – 在不使用标准库函数strlen的情况下查找字符串的长度

/* C Program to find the length of a String without *  using any standard library function  */#include <stdio.h>int main(){    /* Here we are taking a char array of size      * 100 which means this array can hold a string      * of 100 chars. You can change this as per requirement     */    char str[100],i;    printf("Enter a string: \n");    scanf("%s",str);    // '\0' represents end of String    for(i=0; str[i]!='\0'; ++i);       printf("\nLength of input string: %d",i);    return 0;}

输出:

C 程序 查找字符串的长度

相关推荐