수업 후 연습 문제 5.13을 작성하여 두 문자열을 연결한 결과 첫 번째 문자열을 대체합니다.(1) 문자수 그룹을 사용하여stract 함수(즉stract 함수 기능을 가진 함수를 스스로 쓰는 것)를 사용하지 않는다.(2) 표준 라이브러리의stract 함수를 사용한다.

수업 후 연습 문제 5.13을 작성하여 두 문자열을 연결한 결과 첫 번째 문자열을 대체합니다.(1) 문자수 그룹을 사용하여stract 함수(즉stract 함수 기능을 가진 함수를 스스로 쓰는 것)를 사용하지 않는다.(2) 표준 라이브러리의stract 함수를 사용한다.(3)string 방법으로 문자열 변수를 정의합니다.
(1) 문자열을 사용하여 stract 함수를 사용하지 않는다(즉, stract 함수 기능을 가진 함수를 스스로 쓴다).
#include 
#include 
using namespace std;

int main()
{
     
    char s1[80], s2[40];
    int i = 0, j = 0;
    cout << "input string1:";
    cin >> s1;
    cout << "input string2:";
    cin >> s2;
    while (s1[i] != '\0')
        i++;
    while (s2[j] != '\0')
        s1[i++] = s2[j++];
    s1[i] = '\0';
    cout << "The new string is:" << s1 << endl;
    return 0;
}

(2) 표준 라이브러리의 stract 함수를 사용합니다.
#include 
#include
using namespace std;

int main()
{
     
	char s1[80], s2[40];
	cout << "input string1:";
	cin >> s1;
	cout << "input string2:";
	cin >> s2;
	strcat(s1,s2);
	cout << "The new string is:" << s1 << endl;
	return 0;
}

VS2019에서는 소스 파일의 stract를 수정해야 합니다. 따라서 소스 파일은 다음과 같습니다.
#include 
#include
using namespace std;

int main()
{
     
	char s1[80], s2[40];
	cout << "input string1:";
	cin >> s1;
	cout << "input string2:";
	cin >> s2;
	strcat_s(s1,40,s2);
	cout << "The new string is:" << s1 << endl;
	return 0;
}

(3) 스트링 방법으로 문자열 변수를 정의합니다.
#include 
using namespace std;

int main()
{
     
	string s1 = "week", s2 = "end";
	cout << "s1=" << s1 << endl;
	cout << "s2=" << s2 << endl;
	s1 = s1 + s2;
	cout << "The new string is:" << s1 << endl;
	return 0;
}

좋은 웹페이지 즐겨찾기