IT/솔루션) 난 정말C... 없다구요

21장. 실력 다지기 연습문제 03 ) 문제 3 [문자열의 복사]

돔찌 2019. 4. 17. 20:17

<2016. 9. 26. 20:59>

21장. 실력 다지기 연습문제 03 ) 문제 3 [문자열의 복사]

 

/*
배열에 저장되어 있는 영단어를 다른 배열에 복사하는 함수를 다음과 같은 형태로 정의해보자.
이 함수의 정의를 위해서 문제 1에서 정의한 WordLen 함수를 활용하라.
int WordCopy(char scr[], char dest[]); //복사된 단어의 길이 반환
이 함수는 scr로 전달된 영단어를 dest로 전달된 배열의 주소에 복사해야 한다.
*/
#include<stdio.h>
#include<stdlib.h>
#pragma warning(disable : 4996)
int WordCopy(char scr[], char dest[]);
int len(char word[]);
int main(void) {
	char word1[20] = "Orange";
	char word2[20] = "Programming";
	char buf1[20];
	char buf2[20];
	WordCopy(word1, buf1);
	WordCopy(word2, buf2);
	printf("복사본 1 : %s \n", buf1);
	printf("복사본 2 : %s \n", buf2);
	system("pause");
	return 0;
}
int WordCopy(char scr[], char dest[]) {
	int i;
	// int lengh = len(scr);
	for (i = 0; i < len(scr); i++) {
		dest[i] = scr[i];
	}
	dest[i] = '\0';
}
int len(char word[]) {
	int n = 0;
	while (word[n] != '\0')
	  n++;
	return n;
}