728x90
반응형
#include <stdio.h>
#include <ctype.h>
#include <string.h>
void sortCharacters(char* str) {
int length = strlen(str);
char lowercase[length + 1];
char uppercase[length + 1];
int lowercaseIndex = 0;
int uppercaseIndex = 0;
for (int i = 0; i < length; i++) {
if (islower(str[i])) {
lowercase[lowercaseIndex++] = str[i];
} else if (isupper(str[i])) {
uppercase[uppercaseIndex++] = str[i];
}
}
lowercase[lowercaseIndex] = '\0';
uppercase[uppercaseIndex] = '\0';
// 문자열 정렬
for (int i = 0; i < lowercaseIndex - 1; i++) {
for (int j = i + 1; j < lowercaseIndex; j++) {
if (lowercase[i] > lowercase[j]) {
char temp = lowercase[i];
lowercase[i] = lowercase[j];
lowercase[j] = temp;
}
}
}
for (int i = 0; i < uppercaseIndex - 1; i++) {
for (int j = i + 1; j < uppercaseIndex; j++) {
if (uppercase[i] > uppercase[j]) {
char temp = uppercase[i];
uppercase[i] = uppercase[j];
uppercase[j] = temp;
}
}
}
// 결과 출력
printf("소문자만 정렬 : %s\n", lowercase);
printf("대문자만 정렬 : %s\n", uppercase);
}
int main() {
char str[100];
scanf("%s", str);
sortCharacters(str);
return 0;
}
이 코드는 주어진 문자열을 입력 받은 후 소문자와 대문자를 분리하여 각각 정렬합니다. 문자열을 구성하는 각 문자를 검사하여 소문자이면 `lowercase` 배열에, 대문자이면 `uppercase` 배열에 저장합니다. 그리고 각각의 배열을 정렬하는 버블 정렬(bubble sort)을 수행한 후 결과를 출력합니다.
728x90
반응형
'프로그래밍 언어 > C언어' 카테고리의 다른 글
C언어 두 수를 입력 받고 두 수 중에서 더 큰 수를 출력하는 프로그램 (0) | 2023.05.24 |
---|---|
C언어 첫 번째 줄은 1문자이고 두 번째 줄 부터는 1문자 씩 추가되어 출력하는 프로그램 (0) | 2023.05.24 |
C언어 문자열을 입력 받아 대문자와 소문자를 구분하여 각각 순서대로 정렬하는 프로그램 (1) | 2023.05.22 |
C언어 표준입력으로 문자열을 입력 받고 같은 문자열을이어서 두번 출력하는 프로그램 (1) | 2023.05.22 |
임의의 양의 정수 n을 입력 받아 n의 약수의 개수를 출력하는 프로그램 (1) | 2023.05.22 |