char a[50] = " hello world~2013/ 05/ 06 " ;
char *p;
p = strtok(a,"~" );
printf("%s",p); // 결과 = hello world
p = strtok(NULL ,"/ " );
printf("%s",p); // 2013
p = strtok(NULL ,"/ " );
printf("%s",p); // 05
p = strtok(NULL ,"/ " );
printf("%s",p); // 06
접기
#include <iostream> #include <string.h>
using namespace std;
int main() { char a[50]; char b[50]; char *p; int i,temp,age; int TodayYear = 0; int TodayMonth = 0; int TodayDay = 0; int MyYear = 0; int MyMonth = 0; int MyDay = 0; int div1 = 10; int div2 = 1000; cout<<"Today : "; cin>>a; cout<<"Birthday : "; cin>>b;
p = strtok(a,"/"); for(i=0; i<2; i++) { temp = (p[i] - 48) * div1; TodayMonth = TodayMonth + temp; div1 = div1/10; } div1 = 10;
p = strtok(NULL,"/"); for(i=0; i<2; i++) { temp = (p[i] - 48) * div1; TodayDay = TodayDay + temp; div1 = div1/10; } p = strtok(NULL,"/"); for(i=0; i<4; i++) { temp = (p[i] - 48) * div2; TodayYear = TodayYear + temp; div2 = div2/10; } div1 = 10; div2 = 1000;
p = strtok(b,"/"); for(i=0; i<2; i++) { temp = (p[i] - 48) * div1; MyMonth = MyMonth + temp; div1 = div1/10; }
p = strtok(NULL,"/"); div1 = 10; for(i=0; i<2; i++) { temp = (p[i] - 48) * div1; MyDay = MyDay + temp; div1 = div1/10; }
p = strtok(NULL,"/"); for(i=0; i<4; i++) { temp = (p[i] - 48) * div2; MyYear = MyYear + temp; div2 = div2/10; } age = TodayYear - MyYear;
if(TodayMonth > MyMonth) age = age + 0; else if(TodayMonth == MyMonth) { if(TodayDay >= MyDay) age = age + 0; else age = age - 1; } else age = age - 1;
cout<<"나이는 : "<<age<<endl;
return 0; }
접기