第六題,謝謝各位大佬
uj5u.com熱心網友回復:
#include <iostream>
#include <boost/tokenizer.hpp>
#include<cctype>
int main()
{
int numUpper{ 0 };
int numLower{ 0 };
std::string s = "Boost C++ libraries hello 234 World";
boost::char_separator<char> sep(" ");
boost::tokenizer<boost::char_separator<char> > tok(s, sep);
for (auto it = tok.begin(); it != tok.end(); ++it) {
auto firstChar = (*it)[0];
if (isupper(firstChar))
numUpper++;
else if (islower(firstChar))
numLower++;
}
std::cout << "大寫單詞個數:" << numUpper << std::endl;
std::cout << "小寫單詞個數:" << numLower << std::endl;
//大寫單詞個數:3
//小寫單詞個數:2
system("pause");
}
uj5u.com熱心網友回復:
大佬 還有比較簡單一點的嗎,我們才開始學C語言沒多久,這些結構還沒學到
uj5u.com熱心網友回復:
有窮自動狀態機
void main()
{
// 連續多個空格視為一個;數字不在統計內,“,”等符號不作為分割符
TCHAR * szString = _T("Hello world aabbc Hehe haha 783279 abc,ere afdsewre");
int state = 1;
int UpperWordsNumb = 0;
int LowerWordsNumb = 0;
for( LPCTSTR pStr = szString; *pStr != _T('\0'); ++pStr )
{
switch( state )
{
case 1:
if( *pStr >= _T('A') && *pStr <= _T('Z')
{
++UpperWordsNumb;
state = 2;
}
else if( *pStr >= _T('a') && *pStr <= _T('z'))
{
++LowerWordsNumb;
state = 2;
}
break;
case 2:
if( *pStr == _T(' ') || *pStr == _T('\t') || * pStr == _T('\r') || *pStr == _T('\n'))
state = 1;
break;
}
}
printf(_T("大寫單詞個數:%d\r\n小寫單詞個數:%d", UpperWordsNumb, LowerWordsNumb ));
}
uj5u.com熱心網友回復:
剛學C語言,上面的代碼確實難為你了,看這個吧,上面那些就不看了.
#include <stdio.h>
#include <conio.h>
int main() {
char ch;//字符獲取
int big = 0, small = 0;//統計
do {
ch = getchar();//獲取首字母
if (ch >= 'A'&&ch <= 'Z')//判斷首字母是否大寫
++big;
else
++small;
while ((ch = getchar()) != ' ' && ch != '\n');//判斷完詞首后,這里來讀取掉剩余的單詞,直至遇到空格或者回車結束
} while (ch != '\n');//如果不是遇到回車就繼續讀取下一個單詞詞首
printf("詞首是大寫單詞的個數:%d", big);//統計完畢,開始輸出
printf("詞首是小寫單詞的個數:%d", small);
}
uj5u.com熱心網友回復:
非常感謝大佬
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/25889.html
標籤:C語言
