회문이란? 거꾸로 읽어도 원래 읽은 것과 똑같은 단어
level, mom, eye 등
문제 : 단어를 입력받아 회문이면 1, 회문이 아니면 0을 출력함. (제한 : 단어 길이는 3~10)
코드
//SWEA1989 초심자의 회문검사 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int check(char* w);
int main(void){
	int testCase, i, *answer;
	char word[11];
	scanf("%d", &testCase);
	answer = (int*)malloc(sizeof(int)*testCase);
	for(i=0; i<testCase; i++){
		scanf("%s", word);
		answer[i] = check(word);
	}
	for(i=0; i<testCase; i++)	printf("#%d %d\n", i+1, answer[i]);
	return 0;
}
int check(char* w){
	int a, b, cnt;
	cnt = strlen(w);
	for(a=0, b=cnt-1; a<cnt/2; a++, b--){
		if(w[a]!=w[b])	return 0;
	}
	return 1;
}check함수를 만들어 회문검사를 해 회문이면 1, 회문이 아니면 0을 리턴해 답 배열에 저장한다.
회문검사는 첫번째 글자와 마지막 글자부터 순서대로 일치하는지 검사한다.
여러 가지 단어를 넣어서 테스트 해 봤는데 오류는 없는 것 같았다.

#include <stdio.h>
#include <string.h>
int main(void) {
    int test_case, T;
    scanf("%d", &T);
    for (test_case = 1; test_case <= T; test_case++) {
        char word[11] = { 0 };
        scanf("%s", &word);
        int len = strlen(word);
        printf("#%d ", test_case);
        for (int i = 0; i <= len/2; i++) {
            if (word[i] != word[len - i - 1]) {
                printf("0\n");
                break;
            }
            else {
                if (i == len / 2) {
                    printf("1\n");
                }
            }
        }
    }
    return 0;
}이 방법은 위와 똑같지만 함수를 쓰지 않은 방법이다.
'SWEA > [D2]' 카테고리의 다른 글
| [C언어] SWEA 1204 최빈수 구하기 (0) | 2020.08.15 | 
|---|---|
| [C언어] SWEA 1284 수도요금경쟁 (0) | 2020.08.15 | 
| [C언어] SWEA 1970 쉬운 거스름돈 (0) | 2020.08.09 | 
| [C언어] SWEA 1926 간단한 369게임 (0) | 2020.07.19 | 
| [C언어] SWEA 1945 간단한 소인수분해 (0) | 2020.07.17 |