전체 코드

📌 1. 문자열 개념

✅ 문자열(String)이란?

  • 문자(char): char 타입의 단일 문자 저장 ('A', 'B', 'C' ...)
  • 문자열(char 배열): 여러 개의 문자들이 null 문자('\0')로 끝나는 형태로 저장됨
  • 배열과 포인터: char* 형태의 문자 배열의 첫 번째 문자 주소를 가리키는 포인터를 사용하여 문자열을 처리 가능

📌 2. 문자열 기본 함수 구현

1️⃣ StrLen (문자열 길이 계산)

int StrLen(const char* str) {
    int length = 0; // 문자열 길이 저장
    while (str[length] != '\0') { // NULL 문자를 만날 때까지 반복
        length++; // 길이 증가
    }
    return length; // 최종 길이 반환
}

설명

  • 문자열이 끝나는 null 문자('\0')를 찾을 때까지 반복하면서 길이를 계산하는 함수
  • 반복문 종료 조건: str[length] != '\0'

📍 함수 호출 예시

char text[] = "Hello";
cout << StrLen(text); // 출력: 5

2️⃣ 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

3️⃣ 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

4️⃣ 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보다 작음)

5️⃣ 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")
  • Swap 방식을 사용하여 처음과 끝을 교환하는 방법으로 구현

📍 함수 호출 예시

char text[] = "Hello";
ReverseStr(text);
cout << text; // 출력: olleH

📌 3. 전체 코드 (메인 함수 포함)

#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;
}

profile
李家네_공부방

0개의 댓글