定义一个函数int Stringl ength(const char* str),其功能是计算str所指向字符串的实际长度(即不包含末尾的结束标志)。然后在主函数中输入一个字符串,并调用 StringLength()函数,最后输出字符串的实际长度。(注意: 不得调用库函数)
2条回答 默认 最新
- CSDN专家-深度学习进阶 2021-06-28 17:44关注
#include <bits/stdc++.h> using namespace std; int Stringlength(const char* str){ int i=0,cnt=0; for(i=0;str[i]!='\0';i++){ cnt++; } return cnt; } int main(){ int i; char a[5]={"2134"}; cout<< Stringlength(a)<<endl; } /*1 2 3 4 5 6 7 8 9 10*/
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决评论 打赏 举报无用 1