ZZULIOJ:1161: 字符串长度(指针专题)

题目描述

编写一函数len,求一个字符串的长度,注意该长度不计空格。要求用字符指针实现。在主函数中输入字符串,调用该len函数后输出其长度。
int len(char *sp)
{
//实现sp所指串的长度,不计空格。
}

 

输入

输入一个字符串,以回车结束,长度不超过100。

输出

输出一个整数,单独占一行。

样例输入 Copy
What day is today?
样例输出 Copy
15

源代码 

#include <iostream>
#include <cstring>
using namespace std;
int len(char *sp)
{
    int ans = 0;
    for(int i = 0;i < strlen(sp);i ++ )
    {
        if(sp[i] == ' ')continue;
        else ans ++ ;
    }
    return ans;
}
int main()
{
    char a[110];
    gets(a);
    cout << len(a) << endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/couchpotatoshy/article/details/126110525