본문 바로가기

ETC Programmings

거꾸로 해도 같은지 검사하는 프로그램

336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

 

int getLength(char *str){

        int count = 0;

        for(;;){

               if (*str == '\0')

                       break;

               else {

                       count++;

                       str++;

               }

        }

        return count;

}

 

int compareStr(char *str1, char *str2){

        int i;

        int isSame = 0;

        for(i = 0 ; i < getLength(str1) ; i++){

               //printf("%c %c 의비교", str1[i], str2[i]);

               if (str1[i] == str2[i])

                       isSame = 1;

               else

                       isSame = 0;

        }

        return isSame;

}

int compareReverse(char *str){

        int i = getLength(str) - 1;

        int j;

        char *reverse;

        reverse = (char*) calloc(getLength(str),sizeof(char));

 

        for(j = 0 ; i >=0  ; i--, j++)

               reverse[j] = str[i];

       

        return compareStr(str, reverse);

}

 

int main(void){

        char *str = "123213";

        compareReverse(str);

 

        printf("Input string  %s\n",str);

        if(compareReverse(str))

               printf("TRUE");

        else

               printf("FALSE");

}


 


음하하... recursion 시간인데 이러고 있다... recursion으로 바꿔보세~