단어의 오타 수정하기

Yun Young Choi·2022년 9월 5일

CodingTest-C

목록 보기
9/15
post-thumbnail

이런 오타를 내다니. 하수라는 생각이 드는구만.


main 함수 설명

int main() {
    char* words[3] = {"CODE", "COED", "CDEO"};
    int words_len = 3;
    char* word = "CODE";
    int ret = solution(words, words_len, word);
    
    printf("solution 함수의 반환 값은 %d 입니다.\n", ret);
}
  1. char형 포인터 배열 words에 세 개의 오타 선언
  2. int형 오타 세 개의 길이
  3. char형 포인터 변수로 옳은 단어 "CODE" 선언
  4. ret에 solution 반환값 할당

solution 함수 설명

int solution(char* words[], int words_len, char* word) {
    int count = 0;
    
    //빈칸
    
    return count;
}
  1. 수정해야 할 문자 개수를 셀 count 변수 0으로 초기화
  2. 작성하기. . .

빈칸 채우기

int solution(char* words[], int words_len, char* word) {
    int count = 0;
	
		for(int i = 0; i < words_len; i++) {
			for(int j = 0; j < 4; j++) {
				if(words[i][j] != word[j]) {
					count++;
				}
			}
		}
    
    return count;
}

2차원 배열을 이용해야 하는데 그림으로 표현하면 이렇다.


전체 코드

int solution(char* words[], int words_len, char* word) {
    int count = 0;
	
		for(int i = 0; i < words_len; i++) {
			for(int j = 0; j < 4; j++) {
				if(words[i][j] != word[j]) {
					count++;
				}
			}
		}
    
    return count;
}

int solution(char* words[], int words_len, char* word) {
    int count = 0;
    
    return count;
}

실행 결과


profile
안냥하세요

0개의 댓글