pointer?

hi fellows ,
i have a questionYou 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.
here i start the code
#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 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
regard,
sehrish
Last edited on
I can't see why you should use strcat either!
I suggest you declare other 3 char arrays one for the city code one for the country code and one for the number.
in each strtok you can use the strlen to count the number of each token.
If the number is 4 then copy the token to country code array
Else if the number is 3 copy the token to the city code array
Else copy the token to the number array!
closed account (z05DSL3A)
I can't see why you should use strcat either!


hint: Phone number in correct sequence is
hi
i already use strlen after tokinizing but not got require result and now i cannot understand how i copy these because that u telling dionisis i already do , thank for ur help
closed account (z05DSL3A)
Setup an array of char * for your sub-strings.
Use a for loop with strtok() to set the sub-string pointers to the tokens.
Then for each of the sub-strings you can find the length to determine what the token is (switch on length), keeping track again with pointers.
Then finally recombine the correct sequence, with the pointers above and strcat().

Suggested variables:
1
2
3
4
5
6
7
    char input[20]           = {'\0'}; // User input
    char correctSequence[20] = {'\0'}; // correct sequence output
    char * subStr[4]         = {NULL}; // Sub-string tokens
    char * countryCode       = NULL;   // pointer to countryCode sub-string
    char * cityCode          = NULL;   // pointer to cityCode sub-string
    char * number            = NULL;   // pointer to number sub-string
    char * ptr               = NULL;   // general use pointer to char 


Hope that helps
hi,
plz tell me how i use for loop with strtok?this question confused me ?
closed account (z05DSL3A)
something like:
1
2
3
4
5
6
ptr = strtok(input,"-");
for(int x =0; x < 4; ++x)
{
    subStr[x] = ptr;
    ptr = =strtok(NULL,"-");
}
hi work it but cannot understand and get require result as requirement of my question i want to split the string into parts then then store each part into different string . As u can see above i split the string but further i cannot understand how to store these split part into different string plz help me in this regard, or u give me ur code that will provide me help i want to know how to store each part in different string, thanks in advance plz help
closed account (z05DSL3A)
If you feel the need to copy the strings, read up on:
char * strcpy ( char * destination, const char * source );
http://www.cplusplus.com/reference/clibrary/cstring/strcpy/

hi,
thanks grey my problem is solve actually im looking only at hint and never think about strcpy so now i user it and my assignments is solve thanks for ur guaidance , God bless u
solve that assignment or sehrish you if you slove that assignment
sehrish i was also facing the problem in a program of pointers that uses strcpy function, can u pl share your code with us for guidance pl
hi,
husaain i will share my code after due date of my assignment so waite till that time
hope u can understand
hi,
ok hussain as i solve the problem in above u declare seprate strings and after stroke the string u copy the pointer into that string like that
p=strtok(string,"-");
if(p);
strcpy(str1,p);
and similarly the next two
hope u can understand
more if u more confusion then tell what u want to know?
thanks i ve solved my problem.
#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();

}

Isn't it something result oriented
You can customize it in your ways ------------- A little late guys ------------ Good A. Saboor --- 12:03 am

#include<iostream.h>
#include<string.h>
main()
{
char string[20],stringcorrect[20],string1[7],string2[7],string3[7],*p,countrycode;
char *dash="-";
int length;
cout<<"Enter the compelete phone number";
cin>>string;
p=strtok(string,"-");
if(p);
cout<<p<<endl;
strcpy(string1,p);
cout<<string1<<endl;
p=strtok(NULL,"-");
if(p)
cout<<p<<endl;
strcpy(string2,p);
cout<<string2<<endl;
p=strtok(NULL,"-");
if(p)
cout<<p<<endl;
strcpy(string3,p);
cout<<string3<<endl;
cout<<strlen(string1)<<endl;
cout<<strlen(string2)<<endl;
cout<<strlen(string3)<<endl;
if(strlen(string1)==4)
{strcpy(stringcorrect,string1);
strcat(stringcorrect,dash);}
else if(strlen(string2)==4)
{strcpy(stringcorrect,string2);
strcat(stringcorrect,dash);}
else
{strcpy(stringcorrect,string3);
strcat(stringcorrect,dash);}
if(strlen(string1)==3)
{strcat(stringcorrect,string1);
strcat(stringcorrect,dash);}
else if(strlen(string2)==3)
{strcat(stringcorrect,string2);
strcat(stringcorrect,dash);}
else
{strcat(stringcorrect,string3);
strcat(stringcorrect,dash);}
if(strlen(string1)==7)
strcat(stringcorrect,string1);
else if(strlen(string2)==7)
strcat(stringcorrect,string2);
else
strcat(stringcorrect,string3);
cout<<stringcorrect<<endl;
system("pause");
}
Nice to see diffrenent ways, how same problem is addressed, thats the beauty of programming. Best of luck
Here is the solution for Introduction to Programming CS 201 Assignment 3
ENJOYyYYYyYYyYYyY___________________________________________

#include <iostream.h>;
#include <string.h>;
#include <stdlib.h>;

int main()
{
char string[20]; //character input from user
char city[10], country[10], phone[10]; //output containers
char *hold; //reference pointer

cout << "Enter the complete phone number : ";
cin >> string;

// Spliting String

hold=strtok(string,"-"); // <---- string token function

// used loop to store value from pointer to
// there valued containers
do
{

if (strlen(hold)==7) //<----stringlength function
{ strcpy(phone,hold); }

else if (strlen(hold)==4)
{ strcpy(country,hold);}

else
{ strcpy(city,hold); }

hold=strtok(NULL,"-");
}
while (hold != NULL);

// output screen
{cout << "\n\n Country code is = " << country; }
{cout << "\n City code is = " << city; }
cout << "\n 7-digit number is = " << phone;
cout << "\n Phone number in correct sequence is = ";
cout << strcat(country,"-"); //<----string catenation function used
cout << strcat(city,"-") << phone << "\n\n";
system("pause");



}

//alas the sweeeeeeeeeeeeeeeeet end
Topic archived. No new replies allowed.