#include <iostream>
#include <string>
using namespace std;
int Num(char *str)
{
if (str == nullptr)
return 0;
char *ptr = str;
int num = 0, index = 0, n = 0;
while (*ptr != '\0')
{
if (*ptr == ' ')
{
index = num;
}
num++;
ptr++;
}
if (index != 0)
{
index++;
}
for (; str[index] != '\0'; ++index)
++n;
return n;
}
int main(void)
{
string str;
getline(cin, str);
char *str1 = const_cast<char *>(str.c_str());
cout << Num(str1);
return 0;
}
计算字符串最后一个单词的长度,单词以空格隔开
最新推荐文章于 2022-01-19 16:56:12 发布