
기능정리
1. 문자열을 입력받는다.
- 배열로 입력받음
2. 입력받은 문자열에서 단어의 개수를 계산한다.
- 공백이 나타나면 카운트
- 예외처리 : 맨 처음부터 공백일 경우 -1해준다
- 예외처리 : input값이 " " 인 경우 0이 출력되게 해준다
3. 계산한 단어의 개수를 출력한다.
#include <iostream>
#include <string>
using namespace std;
int main() {
int count = 1;
// 문자열을 입력받는다.
// (1) char을 사용한다
char input1[1000000];
cin.getline(input1, 1000000, '\n');
int inputSize = sizeof(input1);
// (2) string을 사용한다.
/*string input2;
getline(cin, input2);*/
// 입력받은 문자열에서 단어의 개수를 계산한다.
for (int i = 0; i < inputSize; i++) {
if (input1[i] == ' ') {
count++;
}
}
// 예외처리 : 맨 처음부터 공백일 경우 -1해준다
if (input1[0] == ' '|| input1[inputSize] == ' ') {
count--;
}
// 예외처리 : input값이 " " 인 경우 0이 출력되게 해준다
if (input1[0] == ' ' && input1[inputSize] == 1) {
cout << "0";
}
//// 계산한 단어의 개수를 출력한다.
else {
cout << count;
}
return 0;
}
왜 안되지 돌겠네
'언어 > C++' 카테고리의 다른 글
| [Programmers/ c++] Level1. 문자열 내 p와 y의 개수 (0) | 2024.02.01 |
|---|---|
| [백준1547] 공 (1) | 2023.12.29 |
| [백준1267] 핸드폰 요금 (0) | 2023.12.28 |
| [백준 1085] 직사각형에서 탈출 (1) | 2023.12.27 |
| [백준 1934 / C++] 최소공배수 (1) | 2023.12.05 |