#include <stdio.h>


int strLen(char *str)

{

int num = 0;


while ((*str != NULL) || (*str != '\0')) {

num++;

str++;

}


return num;

}


// c lib strstr

int strStr(char *str1, char *str2)

{

int strLen1 = strLen(str1);

int strLen2 = strLen(str2);

int i, j;

int count;


for (i = 0; i < strLen1; i++) {

count = 0;

if (*str1 == *str2) {

for (j = 1; j < strLen2; j++) {

if (*(str1 + j) != *(str2 + j)) {

break;

}

count++;

}

if (count == (strLen2 - 1)) {

return i;

//return (i + 1); //for human

}

}

str1++;

}


return -1;

}


// 첫 번째 문자열에서 두 번째 문자열 같은 부분 삭제

char *strStrDel(char *str1, char *str2)

{

int strLen1 = strLen(str1);

int strLen2 = strLen(str2);

int i, j, k;

int count;


char *newStr = (char *)malloc(strLen1 + 1);

if (!newStr)

return NULL;

newStr[strLen1] = '\0';

for (i = 0; i < strLen1; i++) {

*(newStr + i) = *(str1 + i);

}


for (i = 0; i < strLen1; i++) {

count = 0;

if (*str1 == *str2) {

for (j = 1; j < strLen2; j++) {

if (*(str1 + j) != *(str2 + j)) {

break;

}

count++;

}

if (count == (strLen2 - 1)) {

for (k = i; k < strLen1 - strLen2; k++) {

char temp = *(newStr + strLen2 + k);

*(newStr+k) = temp;

}

newStr[strLen1 - strLen2] = '\0';

return newStr;

}

}

str1++;

}


return NULL;


}


// 두 문자열 붙이기

char *strAttach(char *str1, char *str2)

{

int strLen1 = strLen(str1);

int strLen2 = strLen(str2);

int i;


char * newStr = (char *)malloc(strLen1 + strLen2 + 1);

if (!newStr)

return NULL;

newStr[strLen1 + strLen2] = '\0';


for (i = 0; i < strLen1; i++) {

*(newStr + i) = *(str1 + i);

}

for (i = 0; i < strLen2; i++) {

*(newStr + strLen1 + i) = *(str2 + i);

}


return newStr;

}


void main()

{

printf("%s, %s, (%d)\n", "helloWorld", "llo", strStr("helloWorld", "llo"));

printf("%s, %s, (%d)\n", "helloWorld", "llx", strStr("helloWorld", "llx"));

printf("%s, %s, (%d)\n", "hellollxWorld", "llx", strStr("hellollxWorld", "llx"));


printf("%s, %s, (%s)\n", "applepiepizza", "pie", strStrDel("applepiepizza", "pie"));

printf("%s, %s, (%s)\n", "helloWorld", "llo", strStrDel("helloWorld", "llo"));

printf("%s, %s, (%s)\n", "helloWorld", "llx", strStrDel("helloWorld", "llx"));

printf("%s, %s, (%s)\n", "hellollxWorld", "llx", strStrDel("hellollxWorld", "llx"));


printf("%s, %s, %s (%s)\n", "Hello, ", "applepiepizza", "pie", strAttach("Hello, ", strStrDel("applepiepizza", "pie")));



}

'Programming > C,C++' 카테고리의 다른 글

List (C, Single Linked) / ..ing  (0) 2017.04.18
bubble sort 3 / int  (0) 2017.04.17
간단한 thread 예제  (0) 2017.04.16
C++, const with function [펌]  (0) 2017.04.07
C, const vs 가변인자 [펌]  (0) 2017.04.07
Posted by 루나s
,