C 语言实例

C 语言实例 - 计算字符串长度


C 语言实例 C 语言实例
计算字符串长度。
实例 - 使用 strlen()
#include <stdio.h>
#include <string.h>
 
int main()
{
    char s[1000];
    int len;
 
    printf("输入字符串: ");
    scanf("%s", s);
    len = strlen(s);
 
    printf("字符串长度: %d", len);
    return 0;
}
输出结果为:
输入字符串: runoob
字符串长度: 6


实例 - 不使用 strlen()

#include <stdio.h>
 
int main()
{
    char s[1000], i;
 
    printf("输入字符串: ");
    scanf("%s", s);
 
    for(i = 0; s[i] != ''; ++i);
 
    printf("字符串长度: %d", i);
    return 0;
}
输出结果为:
输入字符串: runoob
字符串长度: 6
原文地址:https://www.cnblogs.com/bytebee/p/8535658.html