티스토리 뷰

문제1. C++의 헤더 선언을 통해 표준함수 strlen, strcat, strcpy, strcmp 를 호출하는 예제를 만들자.

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

int main()
{
	char str[50] = "Welcome to my house!";
	char str1[50] = "Hello~";

	cout << "strlen = " << strlen(str) << endl;  // str의 길이

	strcat(str1, str);  // str1 + str
	cout << "strcat = " << str1 << endl; 

	strcpy(str1, str); // str의 문자열을 str1에 복사
	cout << "strcpy = " << str1 << endl;

	cout << "strcmp = " << strcmp(str, str1) << endl; // str1, str 비교 

	return 0;
}



문제2. rand, srand, time 세 함수를 이용하여 0이상 10미만의 난수를 총 5개 생성하는 예제를 만들되, C++의 헤더를 선언하여 작성해보자.

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
	srand((unsigned int)time(NULL));

	for (int i = 0; i < 5; i++)
	{
		cout << "난수" << i << " : " << rand()%100 << endl;
	}

	return 0;
}


댓글