반응형

문제 설명

길이가 n이고, "수박수박수박수...."와 같은 패턴을 유지하는 문자열을 리턴하는 함수, solution을 완성하세요. 예를들어 n이 4이면 "수박수박"을 리턴하고 3이라면 "수박수"를 리턴하면 됩니다.

 

제한 조건

  • n은 길이 10,000이하인 자연수입니다.

입출력 예

n return
3 "수박수"
4 "수박수박"

solution.c

char *solution(int n)
{
    // 리턴할 값은 메모리를 동적 할당해주세요.
    char *answer = (char *)calloc((n * 3) + 1, sizeof(char));

    for (int i = 0; i < n / 2; i++)
        strcat(answer, "수박");

    if (n % 2 != 0)
        strcat(answer, "수");

    return answer;
}

프로그래머스 한글 인코딩이 글자당 2바이트인줄 알았는데 3바이트였다..

반응형
반응형

요약

  • C언어 문법에서 구조체 선언 때마다 struct를 쓰기 귀찮다면 구조체 정의 때 typedef 사용
  • C++ 문법에서는 구조체 정의 때 typedef를 안 붙여줘도 구조체 선언 때 Struct 이름으로만 선언할 수 있다. (클래스(Class)도 마찬가지)

 

typedef struct 정의와 struct 정의

차이는 typedef는 별명을 붙여준다.

주로 쓰게될 별명을 Student(코드 9줄) 정의 때만 쓰는 본명은 언더바를 붙여 _Student(코드 3줄)로 정의한다.

 

typedef struct 언제 써야 하나?

왼쪽은 C언어 파일, 오른쪽은 C++ 파일이다.

 

왼쪽 C언어 문법에서는 구조체 정의 때 "struct People"(코드 11줄)로 정의하게 되면 구조체 선언 시 "struct People people"(코드 23줄)과 같이 struct를 붙여줘야 하는데 "typedef struct _Student"(코드 3줄)와 같이 typedef를 붙여주어 정의하면 선언 시 "Student student"(코드 21줄)로 struct를 안 붙이고 선언할 수 있다. 하지만 typedef 없이 정의(코드 11줄) 했는데 Struct 이름만으로 선언(코드 24줄) 하려고 하면 문법상 오류로 표시된다.

 

하지만 오른쪽 C++ 문법에서는 구조체 정의 때 typedef를 안 붙여줘도 구조체 선언 때 Struct 이름으로만 선언할 수 있다. (클래스(Class)도 마찬가지)

 

반응형
반응형

문제 설명

단어 s의 가운데 글자를 반환하는 함수, solution을 만들어 보세요. 단어의 길이가 짝수라면 가운데 두글자를 반환하면 됩니다.

 

제한 사항

  • s는 길이가 1 이상, 100이하인 스트링입니다.

 


입출력 예

s return
"abcde" "c"
"qwer" "we"

 


solution.c

// 파라미터로 주어지는 문자열은 const로 주어집니다. 변경하려면 문자열을 복사해서 사용하세요.
char *solution(const char *s)
{
    // return 값은 malloc 등 동적 할당을 사용해주세요. 할당 길이는 상황에 맞게 변경해주세요.
    char *answer = NULL;

    if (strlen(s) % 2 == 0)
    {
        answer = (char *)calloc(2, sizeof(char));
        answer[0] = s[(strlen(s) / 2) - 1];
        answer[1] = s[strlen(s) / 2];
    }
    else
    {
        answer = (char *)calloc(1, sizeof(char));
        answer[0] = s[strlen(s) / 2];
    }

    return answer;
}

 

반응형
반응형

학생 3명의 이름과 국어, 수학, 영어 점수를 가지고 있는 데이터를 구성한다고 하자

구조체(struct)를 아직 배우지 않았다면 오른쪽 예제 처럼구성을 했을 것이다.

하지만 구조체로 데이터를 구성한다면 왼쪽 예제처럼 구성할 수 있다.

 

지금 당장 보면 코드도 더 많아져서 오른쪽 예제가 더 낫지 않나 싶을 건데 나중에 포인터와 데이터 구조를 배우게 되면 구조체 더 나아가 클래스(class)로 데이터를 구성하는 쪽이 더 유리하다.

 


no_struct.c

#include <stdio.h>

int main()
{
    char student_1_name[10] = "김철수";
    int student_1_korean = 80;
    int student_1_math = 90;
    int student_1_english = 70;

    char student_2_name[10] = "김영희";
    int student_2_korean = 95;
    int student_2_math = 75;
    int student_2_english = 60;

    char student_3_name[10] = "박민석";
    int student_3_korean = 65;
    int student_3_math = 75;
    int student_3_english = 70;

    /* . . . */

    return 0;
}

 


struct.c

#include <stdio.h>
#include <string.h>

typedef struct _Student
{
    char name[10];
    int korean;
    int math;
    int english;
} Student;

int main()
{
    Student student_1;

    strcpy(student_1.name, "김철수");
    student_1.korean = 80;
    student_1.math = 90;
    student_1.english = 70;

    Student student_2;

    strcpy(student_2.name, "김영희");
    student_2.korean = 95;
    student_2.math = 75;
    student_2.english = 60;

    Student student_3;

    strcpy(student_3.name, "박민석");
    student_3.korean = 65;
    student_3.math = 75;
    student_3.english = 70;

    /* . . . */

    return 0;
}

 

반응형

+ Recent posts