#include <stdio.h>
#include <stdlib.h>
void swap(int* a, int* b); // 전달된 정수 2개의 값을 서로 바꿔주는 함수
void selectSort(int* arr, int length); // 선택 정렬(원소가 있을 자리를 선택)
int strLength(const char* s); // 문자열 길이 ('\0'이 있는 곳까지 카운트)
void reverseStr(const char* s, char* output); // 문자열 거꾸로 (위에 함수를 이용해 길이를 알아낸 다음 거꾸로)
int compareStr(const char* s1, const char* s2); // 문자열 비교 (단순 문자열 비교 (xor를 곁들인))
void toUpper(const char* s); // 문자열 대문자 변환 (대문자 - 소문자 = 32 (ASCII 코드))
void toLower(const char* s); // 문자열 소문자 변환 (대문자 - 소문자 = 32 (ASCII 코드))
int intToStr(int n, char* output); // 정수를 문자열로 변환 (주어진 정수를 주어진 문자 포인터에다가 문자로 저장)
void sliceStr(const char* s, int start, int end, char* output); // 문자열 자르기 (문자열를 start부터 end까지 자르기)
void compressString(const char* s); // 문자열 압축 ex) apple -> a1p2l1e1
int main() {
// 전달된 정수 2개의 값을 서로 바꿔주는 것
//int a = 10;
//int b = 20;
//printf("%d %d\n", a, b);
//swap(&a, &b);
//printf("%d %d\n", a, b);
// 선택 정렬
//int arr[9] = { 9, 3, 5, 6, 2, 1, 4, 7, 8 };
//for (int i = 0; i < 9; i++) {
// printf("%d ", arr[i]);
//}
//printf("\n");
//selectSort(arr, 9);
//for (int i = 0; i < 9; i++) {
// printf("%d ", arr[i]);
//}
//printf("\n");
// 문자열 길이
//printf("%d\n", strLength("abc"));
// 문자열 거꾸로
//backWord("abc");
// 문자열 비교
//int flag;
//flag = compareStr("banana", "banann");
//printf("%d\n", flag);
//flag = compareStr("apple", "apple");
//printf("%d\n", flag);
// 문자열 대문자 변환
//printf("%s\n", toUpper("banana"));
// 문자열 소문자 변환
//printf("%s\n", toLower("BANANA"));
// 문자열 자르기
//char output[4];
//sliceStr("banana", 0, 3, output);
//for (int i = 0; i < 3; i++) {
// printf("%c", *(output + i));
//}
//printf("\n");
// 정수를 문자열로 변환 (밑 함수 구현에 들어감(숫자가 2자리 넘을 경우 해당))
//int n = 123456789;
//char output[10];
//int count = intToStr(n, output);
//for (int i = 0; i < count; i++) {
// printf("%c", *(output + i));
//}
//printf("\n%d\n", count);
// 문자열 압축 ex) apple -> a1p2l1e1
compressString("aaaaaaaaaabbbcc");
return 0;
}
void compressString(const char* s) {
char* str = (char*)malloc(sizeof(char) * (strLength(s) + 1)*2);
if (str == NULL) {
printf("메모리 할당 실패\n");
return;
}
char re = *(s); // 현재 기억하는 문자
int reCount = 1; // 현재 기억하는 문자 개수
int strCount = 0; // 압축된 문자열의 인덱스 카운트
for (int i = 1; i < strLength(s) - 1; i++) {
if (*(s+i) == re) {
reCount++;
}
else {
*(str + strCount++) = re;
strCount += intToStr(reCount, str + strCount); // 리턴 하는 값은 정수의 자릿수
re = *(s + i);
reCount = 1;
}
}
*(str + strCount++) = re;
strCount += intToStr(reCount, str + strCount); // 리턴 하는 값은 정수의 자릿수
*(str + strCount) = '\0';
printf("%s\n", str);
free(str);
}
int intToStr(int n, char* output) {
int num; // 10으로 나눈 나머지(1번째 자리)
int count = 0; // 자릿수
while (1) {
if (n <= 9) {
*(output + count) = n + 48;
*(output + ++count) = '\0';
break;
}
else {
num = n % 10;
n /= 10;
*(output + count) = num + 48;
}
count++;
}
// 수가 거꾸로 부터 저장되어 있기에 다시 거꾸로 저장
char* temp = (char*)malloc(sizeof(char) * (count + 1));
if (temp == NULL) {
printf("메모리 할당 실패\n");
return 0;
}
sliceStr(output, 0, count, temp);
reverseStr(output, temp);
for (int i = 0; i < count; i++) {
*(output + i) = *(temp + i);
}
free(temp);
return count;
}
void sliceStr(const char* s, int start, int end, char* output) {
int count = 0;
for (int i = start; i < end; i++) {
*(output + count) = *(s + i);
count++;
}
*(output + count) = '\0';
}
void toUpper(const char* s) {
int count = 0;
char ch;
char* str = (char*)malloc(sizeof(char) * (strLength(s) + 1));
if (str == NULL) {
printf("메모리 할당 실패\n");
return;
}
while (1) {
ch = *(s + count);
if (ch == '\0') {
*(str + count) = '\0';
break;
}
if (ch >= 'a' || ch <= 'z') {
*(str + count) = ch-32;
}
else {
*(str + count) = ch;
}
count++;
}
printf("%s\n", str);
free(str);
}
void toLower(const char* s) {
int count = 0;
char ch;
char* str = (char*)malloc(sizeof(char) * (strLength(s) + 1));
if (str == NULL) {
printf("메모리 할당 실패\n");
return;
}
while (1) {
ch = *(s + count);
if (ch == '\0') {
*(str + count) = '\0';
break;
}
if (ch >= 'A' || ch <= 'Z') {
*(str + count) = ch+32;
}
else {
*(str + count) = ch;
}
count++;
}
printf("%s\n", str);
free(str);
}
int compareStr(const char* s1, const char* s2) {
int count = 0;
while (1) {
char c1 = *(s1 + count);
char c2 = *(s2 + count);
if (c1 == c2) {
int isEnd1 = (c1 == '\0');
int isEnd2 = (c2 == '\0');
if (isEnd1 || isEnd2) {
if (isEnd1 && isEnd2) {
return 1;
}
else if ((isEnd1 && !isEnd2) || (!isEnd1 && isEnd2)) { // xor 둘 중에 하나만 끝났을 경우(\0)
return 0;
}
}
count++;
}
else {
return 0;
}
}
}
int strLength(const char* s) {
int count = 0;
while (1) {
if (*(s + count) == '\0') {
break;
}
count++;
}
return count;
}
void reverseStr(const char* s, char* output) {
int l = strLength(s);
int count = 0;
for (int i = l - 1; i >= 0; i--) {
*(output+count++) = *(s + i);
}
}
void selectSort(int* arr, int length) {
for (int i = 0; i < length - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < length; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
if (minIndex != i) {
swap(&arr[i], &arr[minIndex]);
}
}
}
void swap(int* a, int* b) {
int temp;
temp = *a;
*a = *b;
*b = temp;
}