//http://blog.nuwana.com
//
//문자열 처리 함수 구현
#include <stdio.h>
#define Max_Size 50
int index(char *, char *); //특정 문자열 위치
int length (char *); //문자열 길이
char *substr(char *, int , int ); //특정위치 문자열
char *translate(char *, char , char ); //문자열 교체
int verify(char *, char *); //a에는 있으나 b에는 없는 첫번째 문자열 위치
void main(void) {
char *test_string = "abcdefg";
printf("test_string => %s\n\n",test_string);
printf("index(test_string,'a') => %d\n",index(test_string,"a"));
printf("length(test_string) %d\n",length(test_string));
printf("substr(test_string, 5, 2)) => %s\n",substr(test_string, 5, 2));
printf("translate(test_string, 'a', 'c')) => %s\n", translate(test_string, 'a', 'c'));
printf("verify(test_string,'abcfg') => %d\n",verify(test_string,"abcfg"));
}
int index(char *string, char *pat) {
int i, j, start=0;
int lasts = length(string) - 1;
int lastp = length(pat) - 1;
int endmatch = lastp;
for(i=0; endmatch <=lasts; endmatch++, start++) {
if(string[endmatch] == pat[lastp])
for(j=0,i=start; j<lastp && string[i] == pat[j]; i++,j++)
;
if(j==lastp)
return start;
}
return -1;
}
int length(char *str)
{
int i=0;
for(i=0 ; *(str+i)!='\0' ; i++);
return i;
}
char *substr(char *str, int st_point, int number) {
int i,j;
static char temp[Max_Size];
for(i=(st_point-1), j=0; i < (st_point+number-1); i++, j++) {
temp[j] = str[i];
}
return (temp);
}
char *translate(char *str, char source, char dest) {
int i;
static char temp[Max_Size];
for(i=0; i < length(str); i++) {
if(str[i] == dest) {
temp[i] = source;
}else{
temp[i] = str[i];
}
}
return (temp);
}
int verify(char *str, char *source) {
int i,j;
int buf[Max_Size];
for(i=0; i<length(str); i++) buf[i] = -1;
for(i=0; i<length(str); i++) {
for(j=0; j<length(source); j++) {
if(str[i] == source[j]) buf[i] = 1;
}
}
printf("verify buf : ");
for(i=0; i<length(str); i++) printf("%3d",buf[i]);
printf("\n",buf[i]);
for(i=0; i<length(str); i++) {
if(buf[i] == -1) return i;
}
return 0;
}
/*
test_string => abcdefg
index(test_string,'a') => 0
length(test_string) 7
substr(test_string, 5, 2)) => ef
translate(test_string, 'a', 'c')) => abadefg
verify buf : 1 1 1 -1 -1 1 1
verify(test_string,'abcfg') => 3
Press any key to continue
*/
//이런거 꼭 숙제로 내주는 사람들 있음 ㅠㅠ;
//Visual C++ 에서 테스트함.