-
백준 1152번 단어의 개수 _ [C++]소프트웨어전공/알고리즘 문제풀이 2021. 9. 25. 23:03
공백을 포함한 문자열을 입력받기 위해서는 getline() 함수가 필요하다.
cin은 공백, 줄바꿈, 태그를 무시하고 데이터를 받기 때문이다.
처음에 cin으로 그것도 char배열을 선언해서 코드를 짜기 시작했는데 전혀 동작하지 않았다.
그리고 추가 조건인 공백으로 시작하거나 끝나는 경우에 대해 어떻게 처리할지 감이 오지 않아서 다른 사람들의 풀이를 찾아보았다.
답안 코드
마지막에 공백이 오는 경우는 고려하지 않기로 해서 애초에 for문이 string의 크기보다 1 작게 들어오게 했다.
첫 시작이 공백인 경우에는 앞선 for문에서 더해진 1을 다시 빼주게끔 조건문을 추가했다.
'소프트웨어전공 > 알고리즘 문제풀이' 카테고리의 다른 글
프로그래머스 폰켓몬 _ [C++] (0) 2021.09.28 C++ 숫자의 총 개수 (0) 2021.09.26 백준 1110번 더하기 사이클 [C++] (0) 2021.09.24 백준 1924번 _ 2007년 x월 y일은 무슨 요일? [C++] (0) 2021.09.23 백준 2741번 _ C++ ( 시간초과 해결하기 ) (0) 2021.09.22