본문 바로가기
IoT 디바이스 활용/C++

C++ - 문자열

by cooluk 2020. 10. 11.

문자열

  • string 클래스

    • 문자열 데이터 저장 및 문자열 처리 함수(메서드) 제공
    • #include <string> 을 먼저 지정 후 사용
      image-20200907113514218
  #include <iostream>
  #include <string>
  using namespace std;
  int main(int argc, char const *argv[])
  {
      string s1 = "Slow", s2 = "steady";
      string s3 = "the race.";
      string s4;
      s4 = s1 + " and " + s2 + " wins " + s3;
      cout << s4 << endl;
      return 0;
  }

Slow and steady wins the race.



  #include <iostream>
  #include <string>
  using namespace std;
  int main(int argc, char const *argv[])
  {
      string s1, addr;
      cout << "이름을 입력하세요: ";    // 홍길동\r\n
      cin >> s1;
      cin.ignore(); // 엔터키 제거

      cout << "주소를 입력하세요: ";
      getline(cin, addr);

      cout << addr << "의" << s1 << "씨 안녕하세요?" << endl;
      return 0;
  }

이름을 입력하세요: 홍길동
주소를 입력하세요: 서울시 종로구
서울시 종로구의홍길동씨 안녕하세요?



image-20200907114203755


  #include <iostream>
  #include <string>
  using namespace std;

  int main(int argc, char const *argv[])
  {
      string s = "When in Rome, do as the Romans.";

      int size = s.size();
      int index = s.find("Rome");

      cout << size << endl;
      cout << index << endl;

      cout << sizeof(s) << endl;  // string 객체의 단위는 항상 32Byte이다.

      s.insert(0, "Hello !! ");  // 맨 앞에 문자열 추가
      cout << s << endl;

      s += " End of World";  // 맨 뒤에 문자열 추가
      // s = s + " End of World";
      cout << s << endl;

      s.append("\n------\n"); // 맨 뒤에 문자열 추가
      cout << s;

      return 0;

  }

31
8
32
Hello !! When in Rome, do as the Romans.
Hello !! When in Rome, do as the Romans. End of World
Hello !! When in Rome, do as the Romans. End of World

------



call by value, call by reference 읽기 쓰기

  #include <iostream>
  #include <string>

  using namespace std;

  int main(int argc, char const *argv[])
  {
      string s = "When in Rome, do as the Romans.";
      // 읽기
      for (auto& ch : s)  // char &ch = s[i]
      {
          cout << ch << ' ';
      }
      cout << endl;

      for (auto ch : s)  // char ch = s[i]
      {
          cout << ch << ' ';
      }
      cout << endl;

      // 쓰기
      for (auto& ch : s)  // char &ch = s[i]
      {
          ch = 'T';
      }
      cout << s << endl;

      for (auto ch : s)  // char ch = s[i]
      {
          ch = 'W';
      }
      cout << s << endl;

      return 0;
  }

W h e n i n R o m e , d o a s t h e R o m a n s .
W h e n i n R o m e , d o a s t h e R o m a n s .
TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT



  #include <iostream>
  #include <string>
  using namespace std;
  int main(int argc, char const *argv[])
  {
      string list[] = {"홍길동", "고길동", "둘리"};
      for (auto &name : list)
      {
          cout << name << endl;
      }
      return 0;
  }

홍길동
고길동
둘리


'IoT 디바이스 활용 > C++' 카테고리의 다른 글

C++ - 생성자와 접근 제한자  (0) 2020.10.12
C++ - 클래스와 객체  (0) 2020.10.11
C++ - 함수  (0) 2020.10.11
C++ - 배열  (0) 2020.10.11
C++ - 제어 구조  (0) 2020.10.09

댓글