문자열 및 문자열 관련 함수
strcpy_s : 문자열 복사
- ”” 안에 문자열을 쓰면 문자열 리터럴이라고 한다.
- utf-8 문자열은 한글 1개당 3바이트로 표현된다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| "문자열 테스트"; // const char 타입으로 20바이트가 잡힌다.
// utf라서 한글 1개당 3바이트로 잡힘.
// const char 배열이 되고, 해당 배열의 시작 주소를 반환하게 되어
// const char* 로 받을 수 있다.
// char* text = "문자열 테스트"; // 에러. "" 문자열은 'const' char 배열이니까.
const char* text = "문자열 테스트"; // 즉 const char 포인터로 받아야 한다.
char text1[20] = "문자열 테스트"; // 이렇게 문자열 배열 초기화가 가능하다. 하지만 문자열을 바꿀 수 없다.
// 문자열 복사 함수
strcpy_s(text1, "text1 테스트다");
printf("text1 = %s\n", text1); // "text1 테스트다" 출력
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| void StringCopy(char* Buffer, const char* Text)
{
int Index = 0;
while (Text[Index] != 0)
{
Buffer[Index] = Text[Index];
++Index;
}
Buffer[Index] = 0;
}
int main()
{
char Text3[128] = {};
StringCopy(Text3, "문자열");
return 0;
}
|
💡 strcpy_s 를 써야 하는 이유?
찾아보기
동적 문자 배열 사용 시 주의점
1
| char* text2 = new char[20];
|
위의 코드에선 동적 문자 배열을 만들고, 그 배열의 시작 주소를 반환해주고 있다.
반환된 배열의 시작 주소는 text2 포인터 변수에 담아두었다.
1
| strcpy_s(text2, text1); // <- 에러. strcpy_s 오버로딩 된 함수의 인자 중 'rsize_t' 인자를 안넣어줘서.
|
text2를 그대로 사용하면, text2 변수는 포인터 변수이기 때문에
동적할당을 했다 해도 함수에서는 그저 포인트 변수로 인식이 된다.
그렇기 때문에 strcpy_s 함수의 오버로딩 된 함수 중에
인자가 (char*, rsize_t, const char*)
타입으로 오버로딩 된 함수가 호출되게 된다.
rsize_t는 typedef 된 size_t이며, unsigned __int64
타입이다.
strlen : 문자열 길이 반환
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| int StringCount(const char* text)
{
int Index = 0;
// 문자열 끝엔 0이 들어가니까
while (Text[Index] != 0)
{
++Index;
}
return Index;
}
int main()
{
int Count = strlen("복사할대상");
Count = StringCount("복사할대상");
printf("Count = %d\n", Count);
}
|
strcat : 문자열 붙이기
문자열 뒤에 문자열을 붙인다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
| void StringCat(char* Buffer, const char* Text)
{
int Index = 0;
while (Buffer[Index] != 0)
{
++Index;
}
int Index1 = 0;
while (Text[Index1] != 0)
{
Buffer[Index] = Text[Index1];
++Index;
++Index1;
}
Buffer[Index] = 0;
}
int main()
{
char Text3[128] = {};
strcpy_s(Text3, "문자열테스트");
strcat_s(Text3, " 문자열붙이기");
printf("Text3 = %s\n", Text3); // "문자열테스트문자열붙이기" 출력
StringCopy(Text3, "문자열");
StringCat(Text3, " 붙이기");
printf("Text3 = %s\n", Text3); // "문자열붙이기" 출력
return 0;
}
|
strcmp : 문자열 비교
- 반환값이 0이면 두 문자열은 같다.
- 음수가 나오면 왼쪽 문자열이 오른쪽문자열보다 사전순으로 앞에 있다.
- 양수가 나오면 오른쪽 문자열이 왼쪽문자열보다 사전순으로 앞에 있다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
| bool StringCmp(const char* Text1, const char* Text2)
{
// 문자열의 길이를 비교하여 같은지 다른지를 판단한다.
int Length1 = StringCount(Text1);
int Length2 = StringCount(Text2);
if (Length1 != Length2)
return false;
// 문자열이 같은지 판단한다.
for (int i = 0; i < Length1; ++i)
{
if (Text1[i] != Text2[i])
return false;
}
return true;
}
int main()
{
int Result = strcmp("문자열", "문자열");
printf("Result = %d\n", Result);
Result = strcmp("문자열2", "문자열1");
printf("Result = %d\n", Result);
Result = strcmp("문자열111", "문자열22");
printf("Result = %d\n", Result);
printf("StringCmp = %d\n", StringCmp("문자열", "문자열"));
return 0;
}
|
strtok : 문자열 분할 (split)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
| int main()
{
char Text4[128] = "공격력, 방어력, 체력";
char* Context = nullptr;
char* TokResult = strtok_s(Text4, ",", &Context);
// 분할된 왼쪽은 TokResult에 들어온다. 나머지 오른쪽은 Context에 들어오게 된다.
printf("Context = %s\n", Context); // " 방어력, 체력" 출력
printf("TokResult = %s\n", TokResult); // "공격력" 출력
//Context = nullptr;
TokResult = strtok_s(nullptr, ",", &Context);
printf("Context = %s\n", Context); // " 체력" 출력
printf("TokResult = %s\n", TokResult); // "방어력" 출력
char Text5[128] = "공격력 : 120";
Context = nullptr;
TokResult = strtok_s(Text5, ":", &Context);
printf("Context = %s\n", Context); // 120 출력
printf("TokResult = %s\n", TokResult); // 공격력 출력
return 0;
}
|
atoi, itoa (_itoa_s), sprintf_s
- atoi : 문자열을 정수형으로 변환
- itoa : 정수형을 문자열로 변환
- sprintf_s : 2, 3번 인자를 이용하여 printf처럼 문자열을 만들어준 후
1번 인자의 문자열 배열에 채워준다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
| int main()
{
char Text5[128] = "공격력 : 120";
Context = nullptr;
TokResult = strtok_s(Text5, ":", &Context);
printf("Context = %s\n", Context); // 120 출력
printf("TokResult = %s\n", TokResult);
// Context에 들어온 120 문자열을 정수 120으로 변환시킨다.
int Attack = atoi(Context);
printf("Attack = %d\n", Attack); // 120 출력
char Text6[64] = {};
//itoa(Attack, Text6, 10);
_itoa_s(Attack, Text6, 10);
printf("Text6 = %s\n", Text6);
char Text7[128] = {};
// 2, 3번 인자를 이용하여 printf처럼 문자열을 만들어준 후에
// 1번인자의 문자열 배열에 채워준다.
sprintf_s(Text7, "Attack = %d", Attack);
printf("Text7 = %s\n", Text7);
return 0;
}
|
댓글남기기