Pointer?

Last Edited By Krjb Donovan
Last Updated: Mar 11, 2014 07:52 PM GMT

Question

hi ,



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 0567 or 333-0092-1234567 or 1234 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 = 0567


Sample Output 2 Enter the complete phone number : 1234

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


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 = 0543


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

  1. include<iostream.h>
  2. 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 output as 1234567-0092-333 then it split it into as 1234567 0092 333 but now further i cannot understand how i use strlen and strcat function and how i store split part in different strings looking for help, thanks in advance

Answer

Hello Sehrish

I don't really understand your assignment. I don't understand Sample 2, and I don't understand how you get "Phone number in correct sequence"

I can help you a little in splitting the string and deciding what the numbers mean based on the length. I hope my example below will help you with your assignment.

Have a look at the program. It is quite short. If there is something you don't understand, please ask again.


  1. include<iostream.h>
  2. include<string.h>

int main() {

   char string[20];
   char *p;
   char *countrycode = "???";
   char *citycode = "???";
   char *phone = "???";
   int length;
   cout<<"Enter the compelete phone number ";
   cin>>string;
   p=strtok(string,"-");
   if (p) 
   {
       do // do in a loop until all parts of the input have been looked at
       {
           length = strlen(p);
           if (length == 7)
           {
               phone = p;
           }
           else if (length == 3)
           {
               citycode = p;
           }
           else if (length == 4)
           {
               countrycode = p;
           }
       } while((p = strtok(NULL, "-")) != NULL); 
   }


   cout << "Country code is " << countrycode << endl;
   cout << "City code is " << citycode << endl;
   cout << "7-digit number is " << phone << endl;
   // I don't know what this means
   cout << "Phone number in correct sequence is = ????" << endl;
   return 0;

}

Advertisement

©2024 eLuminary LLC. All rights reserved.