본문 바로가기
Programming/Algorithm

문자열 입력 받기 c++

by OKOK 2018. 1. 25.

띄어쓰기가 아니라 계속해서 문자열을 받는 것입니다. string 을 사용해서 받으면 못 받습니다. 그럼 어떻게 받아야하는가요? getline 을 써서 받아보도록 하겠습니다.  


/*

단어의 개숫 찾기 null 포인트의 개수를 찾으면 되겠습니다. 오케이요. 

*/


#include <iostream>

#include <string>


using namespace std;


string word;

int cnt = 0;


void problem_in() {

getline(cin, word);

}


void solve() {

for (int i = 0; i < word.length(); i++) {

if (word[i] == ' ') {

cnt++;

}

}


if (word[0] == ' ') {

cnt--;

}


if (word[word.length() - 1] == ' ') {

cnt--;

}


cout << cnt+1 << endl;

}


int main(void) {

problem_in();

solve();

return 0;


getline 을 사용하면 오케이요. getline 을 사용하면, 그게 한번에 한 문장을 모두 받을 수 있고, 단순하게 cin 을 사용하면 스페이스 까지 받게 됩니다.