본문 바로가기
Computer Science

[C언어] 문자열을 활용한 여러 함수 만들기

by DuncanKim 2022. 7. 13.
728x90

[C언어] 문자열을 활용한 여러 함수 만들기

 

 

 

1) 주어진 문자열이 원래 문자열의 시작점부터 일치하는 지 확인하는 함수 구현

// start_with
#include <stdio.h>

// c언어의 타입 정의
typedef int bool;
// 이렇게 하면 타입을 정의할 수 있다.

#define true 1 // 값에 대한 별명
#define false 0

int get_length(char* b){
    int length = 0;
    while(1){
        if(b[length] != '\0'){
            length++;
            continue;
        }
        break;
    }
    return length;
}

bool starts_with(char* a, char* b){
    for(int i = 0; i < get_length(b); i++){
        if(a[i] != b[i]){
            return 0;
        }
  }
   return 1;
}

int main(void) {

  bool rs;

  rs = starts_with("abc", "ab");
  printf("rs : %d\n", rs); // 출력 rs : 1

  rs = starts_with("kbs", "kb");
  printf("rs : %d\n", rs); // 출력 rs : 1

  rs = starts_with("mbc", "mc");
  printf("rs : %d\n", rs); // 출력 rs : 0

  return 0;
}

 

 

2) 주어진 문자열이 원래 문자열의 끝지점부터 일치하는 지 확인하는 함수 구현

// ends_with
#include <stdio.h>

typedef int bool;

#define true 1
#define false 0

int get_str_len(char* str) {
  for ( int i = 0; true; i++ ) {
    if ( str[i] == '\0' ) {
      return i;
    }
  }
}

bool ends_with(char* a, char* b){
    int i = get_str_len(a) - 1;
    int j = get_str_len(b) - 1;

    while(1){
        if(a[i] != b[j]){
            return 0;
        }else{
            if(j == 0 && a[i] == b[j]){
                return 1;
            }
            i--;
            j--;
            continue;
        }
    }
}

int main(void) {

  bool rs;

  rs = ends_with("abc", "bc");
  printf("rs : %d\n", rs); // 출력 rs : 1

  rs = ends_with("kbs", "kb");
  printf("rs : %d\n", rs); // 출력 rs : 0

  rs = ends_with("kbs", "bs");
  printf("rs : %d\n", rs); // 출력 rs : 1

  rs = ends_with("mbc", "mc");
  printf("rs : %d\n", rs); // 출력 rs : 0

  return 0;
}
*/

 

 

3) 주어진 문자열이 원래 문자열과 일치하는 지 확인하는 함수 구현

//str_equals

#include <stdio.h>
#define true 1
#define false 0

typedef int bool;

bool get_str_len(char* str) {
  for ( int i = 0; true; i++ ) {
    if ( str[i] == '\0' ) {
      return i;
    }
  }
}

bool str_equals(char* a, char* b){
    int len_a = get_str_len(a) - 1;
    int len_b = get_str_len(b) - 1;

    if(len_a != len_b){
        return false;
    }

    while(1){
        if(a[len_a] != b[len_b]){
            return false;
        }else{
            if(len_a == 0 && a[len_a] == a[len_b]){
                return true;
            }
            len_a--;
            len_b--;
            continue;
        }
    }
}

int main(void) {
    char* str1 = "abc";
    char* str2 = "abc";
    char* str3 = "abcd";
    char* str4 = "bbc";
    char str5[] = "abc";

    printf("`%s` is equals to `%s` : %d\n", str1, str1, str_equals(str1, str1));
    // 출력 => `abc` is equals to `abc` : 1

    printf("`%s` is equals to `%s` : %d\n", str1, str2, str_equals(str1, str2));
    // 출력 => `abc` is equals to `abc` : 1

    printf("`%s` is equals to `%s` : %d\n", str1, str3, str_equals(str1, str3));
    // 출력 => `abc` is equals to `abcd` : 0

    printf("`%s` is equals to `%s` : %d\n", str1, str4, str_equals(str1, str4));
    // 출력 => `abc` is equals to `bbc` : 0

    printf("`%s` is equals to `%s` : %d\n", str1, str5, str_equals(str1, str5));
    // 출력 => `abc` is equals to `abc` : 1

    return 0;
}

 

 

4) 주어진 문자열이 매개변수로 주어진 두 정수 인덱스 사이에 위치하는 지 확인하는 함수 구현

// start_parts_equal

#include <stdio.h>

typedef int bool;
#define true 1
#define false 0

bool str_part_equals(char* a, int start, int end, char* b){
    int j = 0;
    for(int i = start; i < end; i++){
        if(a[i] != b[j]){
            return false;
        }
        j++;
    }
    return true;
}

int main(void) {
  printf("str_part_equals(\"abcd\", 0, 2, \"ab\") : %d\n", str_part_equals("abcd", 0, 2, "ab"));
  // 출력 => str_equals("abcd", 0, 2, "ab") : 1

  printf("str_part_equals(\"abcd\", 1, 2, \"b\") : %d\n", str_part_equals("abcd", 1, 2, "b"));
  // 출력 => str_equals("abcd", 1, 2, "b") : 1

  printf("str_part_equals(\"abcd\", 2, 2, \"\") : %d\n", str_part_equals("abcd", 2, 2, ""));
  // 출력 => str_equals("abcd", 2, 2, "") : 1

  printf("str_part_equals(\"abcd\", 2, 4, \"cb\") : %d\n", str_part_equals("abcd", 2, 4, "cb"));
  // 출력 => str_equals("abcd", 2, 4, "cb") : 0

  printf("str_part_equals(\"abcd\", 2, 4, \"cd\") : %d\n", str_part_equals("abcd", 2, 4, "cd"));
  // 출력 => str_equals("abcd", 2, 4, "cd") : 1

  return 0;
}

 

 

5) 문장에서 특정 문장의 위치(첫번째 문자의)를 반환하는 함수 구현

// 문장에서 특정 문장의 위치를 반환하는 함수를 만들어주세요.(get_in)

#include <stdio.h>

int get_length(char* a){
    int i = 0;
    while(1){
        if(a[i] == '\0'){
            return i;
        }
        i++;
    }
}

int get_index_of_str(char* a, char* b){
    int i = 0;

        // find start-point (=index)
    for(int j = 0; j < get_length(a); j++){
        if(a[j] == b[0]){
            i = j;
            break;
        }else{
            if(j == get_length(a) - 1){
                return -1;
            }
            continue;
        }
    }

        // printf에 쓰일 index
    int index = i;

        // 주어진 문자열(b)이 원본 문자열(a)의 연속적인 부분 문자열인지 확인 
    for(int k = 0; k < get_length(b); k++){
                // 하나라도 같지 않다면 -1 return
        if(a[i] != b[k]){
            return -1;
        }else{
                        // 연속적인 부분 문자열이 맞는 경우(부분 문자열의 끝까지 탐색 끝)
            if(k == get_length(b) - 1 && a[i] == b[k]){
                return index;
            }
                        // 연속적인 부분 문자열이 맞는 경우(부분 문자열의 끝까지 탐색 중) continue
            i++;
            continue;
        }
    }
    return -1;
}

int main(void) {
  int index;

  index = get_index_of_str("abc", "b");
  printf("index : %d\n", index);
  // 출력 => index : 1

  index = get_index_of_str("test", "es");
  printf("index : %d\n", index);
  // 출력 => index : 1

  index = get_index_of_str("abcd", "bd");
  printf("index : %d\n", index);
  // 출력 => index : -1

  return 0;
}
728x90

'Computer Science' 카테고리의 다른 글

[C언어] 구조체 배열, 구조체 포인터 배열  (0) 2022.07.19
[C언어] C언어 구조체(struct)  (0) 2022.07.13
[C언어] 문자열 활용하기  (0) 2022.07.12
[CS] 결합도와 응집도  (0) 2022.07.12
[CS] 테스트와 TDD  (0) 2022.07.09

댓글