문제 설명
두 배열이 얼마나 유사한지 확인해보려고 합니다. 문자열 배열 s1과 s2가 주어질 때 같은 원소의 개수를 return하도록 solution 함수를 완성해주세요.
s1안에 문자열을 하나씩 가져와 s2안에 있는 문자열과 비교한다.
문자열을 비교하는 함수인 strcmp를 활용하여 같으면 answer의 값을 하나씩 증가시킨다.
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h> // for strcmp function
int solution(const char* s1[], size_t s1_len, const char* s2[], size_t s2_len) {
int answer = 0;
for (int i = 0; i < s1_len; i++) {
for (int j = 0; j < s2_len; j++) {
if (strcmp(s1[i], s2[j]) == 0) {
answer++;
}
}
}
return answer;
}'Coding Test' 카테고리의 다른 글
| 삼각형의 완성조건 (1) (0) | 2024.07.06 |
|---|---|
| 모음 제거 (0) | 2024.07.05 |
| 점의 위치 구하기 (0) | 2024.07.03 |
| 개미 군단 (0) | 2024.07.02 |
| 배열 자르기 (0) | 2024.06.30 |