高质量题解|实现strlen函数
2026-02-02 19:57:16
发布于:北京
31阅读
0回复
0点赞
解题思路
题目要求输出字符串的长度,获取字符串的长度用 s.size() 就行了
代码
#include <bits/stdc++.h>
using namespace std;
int main(){
string s;
cin >> s;
cout << s.size();
return 0;
}
全部评论 1
这样太慢了,建议在输入时就算好长度
#include<cstdio> int main(){ unsigned char strlen(0); while(true){ int chr(std::fgetc(stdin)); if(chr==EOF) break; strlen++; } std::fprintf(stdout,"%hhu",strlen); }2026-07-04 来自 香港
1也可以,但是对新手不友好
2026-07-04 来自 北京
02026-07-04 来自 香港
0这几种都很慢,试试我这个:
#include<stdio.h> int i; char a[101]; int main(){ scanf("%s",a); for(i=1;a[i]!='\0';i++); printf("%d",i); return 0; }2天前 来自 浙江
1













有帮助,赞一个