题目描述:计算字符串最后一个单词的长度,单词以空格隔开。
输入描述:一行字符串
输出描述:整数N,最后一个单词的长度。
输入例子:hello world
输出例子:5
算法实现:#include<iostream>#include<string>using namespace std;/************************************************ * Author: 赵志乾 * Date: 2017-2-16 * Declaration: All Rigths Reserved !!! ***********************************************/int main(){ string instr; getline(cin,instr); int count=0; for(int i=instr.length()-1;i>=0;i--) { if(instr[i]==' ') break; count++; } cout<<count<<endl; return 0;}
新闻热点
疑难解答
图片精选