Menu
CoddyTech

사전 없이 조회하기

Coddy의 Java 개발자를 위한 C 코스 레슨. 48개 중 30번째.

Java Map은 저장, 해싱 및 누락된 키 처리 API를 함께 제공합니다. C17에는 표준 사전 컨테이너가 없습니다. 작은 카탈로그에는 레코드의 명시적 배열과 선형 검색을 사용할 수 있습니다.

C에는 내장 맵이 없습니다. 명시적 표현 방식, 검색 규칙 및 값 누락 시의 처리 방식을 선택하세요.

Java 예제


public class Main {

public static void main(String[] args) {
java.util.Map<String,Integer> counts=new java.util.HashMap<>();counts.put("pen",3);System.out.println(counts.getOrDefault("pen",0));
}
}

Java 출력:

3

C 예제

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include <stdbool.h>
#include <limits.h>
#include <errno.h>

int main(void) {
const char *names[]={"pen","book"};int counts[]={3,2};int result=0;for(size_t i=0;i<2;i++)if(strcmp(names[i],"pen")==0)result=counts[i];printf("%d\n",result);
return 0;
}

C 출력:

3
challenge icon

챌린지

쉬움

고정 목록은 pen=3, book=2, ink=0입니다. stock_of(name)을 구현하여 개수를 반환하고, 정확히 일치하는 ASCII 이름이 없으면 -1을 반환하세요.

잠금 해제된 소스 파일과 헤더 파일을 수정하세요. 이를 직접 호출하는 잠금 상태의 main.c와 선언된 공개 API의 호환성을 유지하세요. main을 추가하지 마세요. 작업의 모든 숫자 입력 데이터는 해당 작업에서 범위를 명시적으로 확인하지 않는 한 오버플로를 피할 수 있을 만큼 작습니다. 지정된 출력만 출력하세요. 드라이버가 입력 파싱과 출력을 보여 주는 경우 이를 제공합니다.

직접 해보기

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include <stdbool.h>
#include <limits.h>
#include <errno.h>
#include "lesson.h"
int main(void){char text[128]="";if(fgets(text,sizeof text,stdin))text[strcspn(text,"\n")]=0;printf("%d\n",stock_of(text));return 0;}
quiz icon실력 점검

이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.

Java 개발자를 위한 C의 모든 레슨

직접 연습해 보세요: 온라인 C 컴파일러