[Absolute C++]Ch2-10

문자열 hate를 love로 바꾸는 문제

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <fstream>
#include <string>

using namespace std;
int main() {
 string text;
 fstream inputStream;

 inputStream.open("test.txt");
 /*
 txt파일의 원래 내용 출력
 */
 while (inputStream >> text) {
  cout << text << " ";

 }
 inputStream.close();
 cout << endl;
 /*
 txt파일에서 hate를 love로 바꾼 내용 출력
 */
 inputStream.open("test.txt");

 while (inputStream >> text) {
  if (text == "hate") { //문자열 비교
   cout << "love" << " ";
   continue;
  }
  else {
   cout << text << " ";
  }
 }
 inputStream.close();

 return 0;
}

댓글