문제
작성한 코드
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LENGTH 11
int compare_strings(const void *a, const void *b) {
return strcmp((const char *)a, (const char *)b);
}
int main() {
int count;
char input[20][MAX_LENGTH];
scanf("%d", &count);
getchar();
for (int i = 0; i < count; i++) {
fgets(input[i], MAX_LENGTH, stdin);
input[i][strcspn(input[i], "\n")] = '\0';
}
qsort(input, count, sizeof(input[0]), compare_strings);
for (int i = 0; i < count; i++) {
printf("%s\n", input[i]);
}
return 0;
}
문자열의 최대 길이가 10인점을 define을 통해 선언해줬고
qsort라는 함수를 이용해서 사전순으로 정렬해주었다.
함수를 쓰지 않고 직접 구현해보고 싶다는 느낌
'C언어 > 문제은행' 카테고리의 다른 글
[10주차] 예비군 파이팅 (0) | 2023.07.26 |
---|---|
[10주차] 숫자 뒤집기 (0) | 2023.07.26 |
[8주차] 행렬 덧셈, 뺄셈 (0) | 2023.07.17 |
[8주차] 90도 회전 (0) | 2023.07.17 |
[8주차] 2차원 배열 입출력 (0) | 2023.07.17 |