SRM 505 Easy SentenceCapitalizerInator

問題

小文字からなる文章が与えられた時,文章の先頭だけ大文字に変更する.

解答

1文字目,もしくはparagraph[i-1]=' ', paragraph[i-2] = '.'の時だけparagraph[i]を大文字にする.

class SentenceCapitalizerInator {
public:
  string fixCaps(string paragraph) {
    string res;
    int n = paragraph.size();
    int i;
    int diff = 'A' - 'a';
    for(i = 0; i < n;){
      char c = paragraph[i];
      if ( i == 0 || (paragraph[i-1] == ' ' && paragraph[i-2] == '.')){
        if ('a' <= c && c <= 'z') c = (char)(c + diff);
        res += c;
      }else{
        res += c;
      }
      i++;
    }
    return res;
  }
};