个人思路:
先读取整行的输入到字符串s中,先设标题的长度就是整个字符串的长度
再遍历整个字符串,如果遇到了空格和换行,再将总长度-1即可;
遍历完整个字符串后即可得到标题长度.
#include<bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
string s;
getline(cin,s);
int ans=s.length();
for(char i:s){
if(i==' '){
ans--;
}else if(i=='\n'){
ans--;
}
}
cout<<ans;
}

京公网安备 11010502036488号