본문 바로가기

Beakjoon/else

[백준] 4458번 첫 글자를 대문자로 (C++)

문제

https://www.acmicpc.net/problem/4458

 

4458번: 첫 글자를 대문자로

첫째 줄에 줄의 수 N이 주어진다. 다음 N개의 줄에는 문장이 주어진다. 각 문장에 들어있는 글자의 수는 30을 넘지 않는다. 모든 줄의 첫 번째 글자는 알파벳이다.

www.acmicpc.net

코드

#include <iostream>
#include <string>

using namespace std;

int main(){
  int n;
  cin >> n;
  cin.ignore();
  for(int i=0; i<n; i++){
    string s;
    getline(cin, s);
    s[0] = toupper(s[0]);
    cout << s << '\n';
  }
  return 0;
}

정리

cin과 getline()을 함께 사용할 때 cin.ingore()이 필요한 이유

→ cin은 '\n'을 변수에 담지 않고 입력버퍼에 남겨두고 getline은 '\n'을 변수에 담기 때문이다.

 

++

toupper(); 대문자로 변환

tolower(); 소문자로 변환

참조

https://artist-developer.tistory.com/29

https://namwhis.tistory.com/entry/cin%EA%B3%BC-getline%EC%9D%84-%EA%B0%99%EC%9D%B4-%EC%82%AC%EC%9A%A9%ED%95%A0%EB%95%8C-cinignore%EC%9D%B4-%ED%95%84%EC%9A%94%ED%95%9C-%EC%9D%B4%EC%9C%A0-%EA%B8%B0%EB%A1%9D