#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int CheckSpace(char * ptr) //공백 위치 확인 -> 정수로 반환, 공백위치: str[i]
{
int i = 0;
while(ptr[i] != ' ')
i++;
return i;
}
int main(void)
{
char str1[20];
char str2[20];
printf("이름과 나이 입력 1(구분 공백): ");
fgets(str1, sizeof(str1), stdin);
printf("이름과 나이 입력 2(구분 공백): ");
fgets(str2, sizeof(str2), stdin);
if(strncmp(str1, str2, CheckSpace(str1))==0)
printf("이름 동일 \n");
else
printf("이름 불일치 \n");
int age1 = atoi(&str1[CheckSpace(str1)+1]); //공백위치 +1 한 값부터 나이 시작
int age2 = atoi(&str2[CheckSpace(str2)+1]); // &로 나이값 시작 주소값 반환
if(age1 == age2)
printf("나이 동일 \n");
else
printf("나이 불일치 \n");
}
댓글