Solution 01
Code:
#include <iostream>
#include <cstring>
using namespace std;
main(){
char Directory[16],part1[3],part2[4], part3[7];
char *Tptr;
int size;
cout<<"Enter the complete phone number (as \"0092-123-1234567\")
:";
cin>>Directory;
Tptr=strtok(Directory, "-");
while(Tptr!=NULL ){
cout<<Tptr<<"\n";
size=strlen(Tptr);
switch(size){
case 4:
strcpy(part2, Tptr);
break;
case 3:
strcpy(part1, Tptr);
break;
case 7:
strcpy(part3, Tptr);
break;
}
Tptr=strtok(NULL,"-");
}
cout<<endl<<"Country Code is ="<<part2<<"\nCity code is
="<<part1<<"\n7-digit number is = "<<part3<<"\nPhone number in
correct sequence is ="<<part2<<"-"<<part1<<"-"<<part3;
system("Pause");}
Solution 2
Code:
#include <iostream>
#include <cstring>
#include <string.h>
#include <conio.h>
using namespace std;
main(){
char Directory[16],part1[3],part2[4], part3[7], *Tptr;
int size;
cout<<"Enter the complete phone number :";
cin>>Directory;
Tptr=strtok(Directory, "-");
do{
size=strlen(Tptr);
switch(size){
case 4:
strcpy(part2, Tptr);
break;
case 3:
strcpy(part1, Tptr);
break;
case 7:
strcpy(part3, Tptr);
break;
}
Tptr=strtok(NULL,"-");
}while(Tptr!=NULL );
cout<<endl<<"Country Code is ="<<part2<<"\nCity code is
="<<part1<<"\n7-digit number is = "<<part3<<"\nPhone number in
correct sequence is ="<<part2<<"-"<<part1<<"-"<<part3;
getch();}


Sponsored Links