字符串最后一个单词的长度

xiaoxiao2021-02-28  89

题目描述

 计算字符串最后一个单词的长度,单词以空格隔开。

输入描述

一行字符串,非空,长度小于5000;

输出描述

整数N,最后一个单词的长度;

实例

输入

hello world

输出

5

代码:

#include <iostream>

#include <vector>

using namespace std;

int main(){

string str;

int count = 0;

while(getline(cin,str)){

int i = 0;

while(str[i] != '\0'){

if(str[i] == ' ')

count   = 0;

else 

count ++;

i++;

}

cout<<count<<endl;

}

return 0;

}

转载请注明原文地址: https://www.6miu.com/read-28486.html

最新回复(0)