0

i'm new here and i have a question regarding pointers that is here
You are required to write a program that takes a phone number as input from the user and stores it as a string value. The phone number will consist of country code, city code and actual 7-digit number separated by “-”. User can enter the phone number in any order. Your program should be able to recognize the country code, city code and 7-digit number and display it in the following format.
Country code - City code – 7-digit number


Detailed Description:

1. Take phone number as input from the user.
2. The number should be stored as a string value.
3. User can enter the phone number in any order for example 0092-333-1234567 or 333-0092-1234567 or 1234567-333-0092 etc.
4. Program should be able to recognize country code, city code and 7-digit number from the string and display it in the right sequence.


Sample Output 1
Enter the complete phone number : 0092-1234567-333

Country code is = 0092
City code is = 333
7-digit number is = 1234567
Phone number in correct sequence is = 0092-333-1234567

Sample Output 2
Enter the complete phone number : 1234567-321-0092

Country code is = 0092
City code is = 321
7-digit number is = 1234567
Phone number in correct sequence is = 0092-321-1234567


Sample Output 3
Enter the complete phone number : 300-0092-9876543

Country code is = 0092
City code is = 300
7-digit number is = 9876543
Phone number in correct sequence is = 0092-300-9876543

HINTS:
You can split the string into three parts and store each part as different string.
You should use strtok, strlen, and strcat functions
i work on it and the code that i start is here below

#include<iostream.h>
#include<string.h>
main()
{
      char string[20],*p,countrycode;
      int length;
      cout<<"Enter the compelete phone number";
      cin>>string;
      p=strtok(string,"-");
      if(p);
      cout<<p<<endl;
      p=strtok(NULL,"-");
      if(p)
      cout<<p<<endl;
      p=strtok(NULL,"-");
      if(p)
      cout<<p<<endl;
      }

and if i give it input as 1234567-333-0092 then it give outpur as
1234567
333
0092
but now further i do not know now how can i store these splitting parts into different string and how i use strlen and strcat function plz guide me
thanks in advance

Edited by Ezzaral: Added code tags. Please use them to format any code that you post.

4
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by daviddoria
0

why don't you try to use strlen to get the string length for three outputs, then catenate it using strcat in sequence of string length of 4, 3, and 7?

-4
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>



void main()

{
clrscr();
char ch,*str,*country,*city,*digit;

cout<<"Enter the complete phone number: "<<endl;



gets(str);


cout<<str<<endl;
 char *first=strtok(str,"-");
// cout<<first<<endl;
 char *second=strtok(NULL,"-");
//cout<<second<<endl;
 char *third=strtok(NULL,"-");
//cout<<third<<endl;




if(strlen(first)==4 && strlen(second)==3)

  {

	country=first;

	city=second;
	digit=third;

  }



else if(strlen(first)==3 && strlen(second)==4)

  {

	city=first;

	country=second;

	digit=third;

  }



else if(strlen(first)==7 && strlen(second)==4)

  {

	digit=first;

	country=second;

	city=third;

  }



else if(strlen(first)==7 && strlen(second)==3)

  {

	digit=first;

	city=second;

	 country=third;

  }


else if(strlen(first)==4 && strlen(second)==7)

  {

	country=first;

	digit=second;

	 city=third;

  }

else if(strlen(first)==3 && strlen(second)==7)

  {

	city=first;

	digit=second;

	 country=third;

  }


cout<<"Country code is :"<<country<<endl;

cout<<"City code is :"<<city<<endl;

cout<<"7-digit number is :"<<digit<<endl;

cout<<"Phone number in correct sequence is: "<<country<<"-"<<city<<"-"<<digit<<endl;



getch();

}

Edited by __avd: Added [code] tags. For easy readability, always wrap programming code within posts in [code] (code blocks).

Comments
Don't hijack someone elses thread with this crap
This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.