Menu Close

输入一个字符串,计算出字符串长度 ( 不能用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;
}

results:

Enter a string:
stupidguy

Length of input string: 9
Process returned 0 (0x0) execution time : 6.459 s
Press any key to continue.

 

READ  找出出两个正整数之间的质数并打印出来
除教程外,本网站大部分文章来自互联网,如果有内容冒犯到你,请联系我们删除!

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

Leave the field below empty!

Posted in C 决策和循环语句, C语言习题集

Related Posts