當前位置:編程學習大全網 - 編程軟體 - 1.輸入15個字符,統計其中英文字母、空格或回車、數字字符和其他字符的個數!用c++編程,求高手,在線等

1.輸入15個字符,統計其中英文字母、空格或回車、數字字符和其他字符的個數!用c++編程,求高手,在線等

#include<iostream>

#include<string>

using namespace std;

void Count(const char *pCh)

{

int char_count = 0;

int number_count = 0;

int space_count = 0;

int other_count = 0;

for(int n = 0; n < 15; n++)

{

if ('A' <= pCh[n] && pCh[n] <= 'Z' || 'a' <= pCh[n] && pCh[n] <= 'z')

char_count++;

else if( '0' <= pCh[n] && pCh[n] <= '9' )

number_count++;

else if(' ' == pCh[n] || '\n' == pCh[n])

space_count++;

else

other_count++;

}

cout<<char_count<<" 個字母"<<endl;

cout<<number_count<<" 個數字"<<endl;

cout<<space_count<<" 個空格或回車"<<endl;

cout<<other_count<<" 個其它字符"<<endl;

}

void main()

{

char chArray[15];

int nIndex = 0;

cout<<"請輸入 15 個字符:"<<endl;

while(nIndex < 15)

{

chArray[nIndex++] = getchar(); // getchar()才能接收空格和回車

}

Count(chArray);

}

  • 上一篇:三菱PLC用模擬量PID+pwm怎樣控制異步正反轉,只要思路就可
  • 下一篇:MFC多線程寫log文件出現錯誤
  • copyright 2024編程學習大全網