char 타입의 단일 문자 저장 ('A', 'B', 'C' ...)'\0')로 끝나는 형태로 저장됨char* 형태의 문자 배열의 첫 번째 문자 주소를 가리키는 포인터를 사용하여 문자열을 처리 가능StrLen (문자열 길이 계산)int StrLen(const char* str) {
int length = 0; // 문자열 길이 저장
while (str[length] != '\0') { // NULL 문자를 만날 때까지 반복
length++; // 길이 증가
}
return length; // 최종 길이 반환
}
✅ 설명
'\0')를 찾을 때까지 반복하면서 길이를 계산하는 함수str[length] != '\0'📍 함수 호출 예시
char text[] = "Hello";
cout << StrLen(text); // 출력: 5
StrCpy (문자열 복사)void StrCpy(char* dest, const char* src) {
int i = 0;
while (src[i] != '\0') {
dest[i] = src[i]; // src에서 dest로 문자 복사
i++;
}
dest[i] = '\0'; // 마지막에 NULL 문자 추가
}
✅ 설명
src(복사할 문자열)를 dest(목적지)에 복사하는 함수'\0'을 추가하여 문자열이 정상적으로 끝나도록 설정📍 함수 호출 예시
char source[] = "Hello";
char destination[10];
StrCpy(destination, source);
cout << destination; // 출력: Hello
StrCat (문자열 덧붙이기)void StrCat(char* dest, const char* src) {
int len = StrLen(dest); // dest의 현재 길이 찾기
int i = 0;
while (src[i] != '\0') {
dest[len + i] = src[i]; // dest 끝에 src의 문자를 추가
i++;
}
dest[len + i] = '\0'; // NULL 문자 추가
}
✅ 설명
dest 문자열 끝을 찾아 src 문자열을 덧붙이는 함수📍 함수 호출 예시
char first[20] = "Hello";
char second[] = " World";
StrCat(first, second);
cout << first; // 출력: Hello World
StrCmp (문자열 비교)int StrCmp(const char* a, const char* b) {
int i = 0;
while (a[i] != '\0' || b[i] != '\0') {
if (a[i] > b[i]) return 1; // a가 크면 1 반환
if (a[i] < b[i]) return -1; // b가 크면 -1 반환
i++;
}
return 0; // 두 문자열이 같으면 0 반환
}
✅ 설명
strcmp() 함수와 동일한 기능을 수행a > b이면 1, a < b이면 -1, 동일하면 0 반환📍 함수 호출 예시
char str1[] = "Apple";
char str2[] = "Banana";
cout << StrCmp(str1, str2); // 출력: -1 (Apple이 Banana보다 작음)
ReverseStr (문자열 뒤집기)void ReverseStr(char* str) {
int len = StrLen(str);
for (int i = 0; i < len / 2; i++) {
char temp = str[i];
str[i] = str[len - 1 - i]; // 양 끝 문자 교환
str[len - 1 - i] = temp;
}
}
✅ 설명
"Hello" → "olleH")📍 함수 호출 예시
char text[] = "Hello";
ReverseStr(text);
cout << text; // 출력: olleH
#include <iostream>
using namespace std;
// 1. 문자열 길이 계산
int StrLen(const char* str) {
int length = 0;
while (str[length] != '\0') {
length++;
}
return length;
}
// 2. 문자열 복사
void StrCpy(char* dest, const char* src) {
int i = 0;
while (src[i] != '\0') {
dest[i] = src[i];
i++;
}
dest[i] = '\0';
}
// 3. 문자열 덧붙이기
void StrCat(char* dest, const char* src) {
int len = StrLen(dest);
int i = 0;
while (src[i] != '\0') {
dest[len + i] = src[i];
i++;
}
dest[len + i] = '\0';
}
// 4. 문자열 비교
int StrCmp(const char* a, const char* b) {
int i = 0;
while (a[i] != '\0' || b[i] != '\0') {
if (a[i] > b[i]) return 1;
if (a[i] < b[i]) return -1;
i++;
}
return 0;
}
// 5. 문자열 뒤집기
void ReverseStr(char* str) {
int len = StrLen(str);
for (int i = 0; i < len / 2; i++) {
char temp = str[i];
str[i] = str[len - 1 - i];
str[len - 1 - i] = temp;
}
}
// 메인 함수
int main() {
const int BUF_SIZE = 100;
char a[BUF_SIZE] = "Hello";
char b[BUF_SIZE] = "World";
cout << "StrLen: " << StrLen(a) << endl; // 5
StrCpy(b, a);
cout << "StrCpy: " << b << endl; // Hello
StrCat(a, b);
cout << "StrCat: " << a << endl; // HelloHello
cout << "StrCmp: " << StrCmp(a, b) << endl; // 비교 결과 출력
ReverseStr(a);
cout << "ReverseStr: " << a << endl; // 뒤집힌 문자열 출력
return 0;
}