华为在线机试-字符串最后一个字符长度

来源:互联网 发布:淘宝手机端设置首页 编辑:程序博客网 时间:2024/06/05 04:56
#include<iostream>#include<string>using namespace std;#define MAX_LEN 5000int str_len(string str){    int count = 0;    for(int index = 0;index < MAX_LEN;index++)    {        if(str[index] != '\0')            count++;        else            break;    }    return count;}int main(){    int len = 0;    string str;    getline(cin, str);    int nReturn = str_len(str);    for(int index = nReturn - 1; index >= 0; index--)    {        if(str[index] != ' ')            len++;        else            break;    }    cout<<len;    return 0;}
阅读全文
0 0
原创粉丝点击