본문 바로가기
IT/C Language

string 문자열 사용

by Jang HyunWoong 2014. 12. 19.

string형 변수와 char형 문자열 배열의 혼합 처리

 

  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. using namespace std;
  5.  
  6. int main() {
  7. // your code goes here
  8. string str1, str2, str3, myString;
  9. char ch[80] = "Just do it.";
  10.  
  11. str1 = "Good ";
  12. str2 = "morning.";
  13. str3 = str1 + str2;
  14. cout << str3 << endl;
  15.  
  16. if(str1 == str2)
  17. cout << "Same" << endl;
  18. else
  19. cout << "Differnt" << endl;
  20.  
  21. myString = ch;
  22. cout << "myString = " << myString << endl;
  23. cout << "ch = " << ch << endl;
  24.  
  25. myString = "Good luck to you.";
  26. strcpy(ch, myString.c_str()); //거꾸로 myString을 배열 ch에 대입한다.
  27.  
  28. cout << "myString = " << myString << endl;
  29. cout << "ch = " << ch << endl;
  30.  
  31. return 0;
  32. }

 

Good morning.
Differnt
myString = Just do it.
ch = Just do it.
myString = Good luck to you.
ch = Good luck to you.


반응형