/* 1.字符串最后一个单词的长度*/ string str; getline(cin, str); int count = 0; int len = str.length(); for (int i = len-1; i > -1; i--) { if (str[i] != ' ') { count++; } else { break; } } cout << count; /* 2.计算某字符出现次数 */ string str_data; char ch; cout << "晴天输入一行由字母,数字和空格组成的字符串:"; cin >> str_data; cout << "输入一个字符:"; cin >> ch; int count = 0; for (auto i = 0; i < str_data.size(); i++) { if (toupper(ch) == toupper(str_data[i])) { count++; } } cout << count;
标签:count,cout,int,++,算法,str,data From: https://www.cnblogs.com/lihongbing1997-1004/p/17089880.html