![[백준 10809번] 알파벳 찾기 (c)](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2Fbz1I84%2FbtrpHGbzdw5%2FnSFNARn6k3gCpNpixyHz9K%2Fimg.png)
[백준 10809번] 알파벳 찾기 (c)알고리즘2022. 1. 3. 14:44
Table of Contents
https://www.acmicpc.net/problem/10809
10809번: 알파벳 찾기
각각의 알파벳에 대해서, a가 처음 등장하는 위치, b가 처음 등장하는 위치, ... z가 처음 등장하는 위치를 공백으로 구분해서 출력한다. 만약, 어떤 알파벳이 단어에 포함되어 있지 않다면 -1을 출
www.acmicpc.net
코드
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h> //EXIT_FAILURE이 정의된 헤더파일
#include <string.h> //strlen()이 정의된 헤더파일
#include <ctype.h> //islower()이 정의된 헤더파일
int main(void)
{
int index; //알파벳의 순서 (즉, a는 index가 0, b는 1)
char word[100];
int include[26]; //알파벳이 포함돼있는지와 등장위치를 담은 배열
scanf("%s", &word);
for (int i = 0; i < 26; i++) {
include[i] = -1;
}
for (int i = 0; i < strlen(word); i++) { // i < strlen(word);를 word[i] != '\0'으로 바꿔도 됨
if (islower(word[i])) { //word[i]가 소문자이면
index = word[i] - 97; //'a'는 아스키코드로 97
if ((include[index]) == (-1)) {
include[index] = i; //i가 등장 위치이므로
}
}
else {
fprintf(stderr, "소문자만 입력하세요");
exit(EXIT_FAILURE);
}
}
for (int i = 0; i < 26; i++) {
printf("%d ", include[i]);
}
return 0;
}
부가 설명
'알고리즘' 카테고리의 다른 글
[백준 2775번] 부녀회장이 될테야 (c) (0) | 2022.01.06 |
---|---|
[백준 1152번] 단어의 개수 (c) (0) | 2022.01.05 |
[백준 1157번] 단어공부 (c) (0) | 2022.01.04 |
[백준 1110번] 더하기 사이클 (C) (0) | 2022.01.02 |
[백준 4344번] 평균은 넘겠지 (C, 이중포인터 이용) (0) | 2022.01.02 |
@blog_bbg :: bbg
포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!