473,320 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

C++ string use strtok, strlen, and strcat

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
May 13 '10 #1
17 4227
Banfa
9,065 Expert Mod 8TB
And where is your attempt to do this?

Also since you are using C++ you would be a lot better off using a istringstream to split out the fields rather than legacy C functions.
May 13 '10 #2
give me solution ;;;;;;;;;;;;;;;;;;.....
May 13 '10 #3
donbock
2,426 Expert 2GB
You must be kidding.
OK, joke's over. Show us the portion of your code where you're stuck and carefully describe the problem you're having with it.
May 13 '10 #4
im taking in this Problem donbok woud you help me ,If i use strtok function to split the numbers that hasbeen entered by a user, then how to rejoin those string part in different order??
May 13 '10 #5
jkmyoung
2,057 Expert 2GB
Figure out which token you want first.
Output that.
Figure out which token you want second.
Output that.
Figure out which token you want third.
Output that.
May 13 '10 #6
the Problem is how to figure out??? if i have splited a line into 3 tokens and i want to place 2nd token at 1st position and 3rd token at 2nd position and 1st token at last position??? How to do that..???? explain please!
May 13 '10 #7
jkmyoung
2,057 Expert 2GB
output token[1]; //2nd token, 1st position
output token[2]; //3rd token, 2nd position
output token[0]; //1st token, 3rd position.
May 13 '10 #8
young im not getting the way you explaining....give some refferrence in c++ according the give problem
May 13 '10 #9
weaknessforcats
9,208 Expert Mod 8TB
So far I haven't seen a bit of your code to solve this.

I suggest you forget your problem and learn how to use strok. Maybe take the string:

Mary had a little lamb

and write code to display each word.

Once you do that you should be able to rearrange the words.
May 13 '10 #10
#include<iostream.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>

int main ()
{
char string1[80];
char * chr;
cout<<" enter you Mobile Number";
cin>> string1;

chr = strtok (string1,"-");
while (chr!= NULL)
{
cout<< "\n"<< chr;
chr = strtok (NULL, "-");
}
}


OUTPUT Enter your Mobile Number: 9999999-3333-777

9999999
3333
777
======================


Now tell me how to arrange them in a right sequence???
May 13 '10 #11
weaknessforcats
9,208 Expert Mod 8TB
First, chr is a pointer to char. Where is the char? Answer: It's in the string that has your mobile number. That is, it points to a char in the string1 array.

Second, you have a pointer to a char so just put the char somewhere. Like in another char:

[code]char first;

In your loop...

first = *chr; /*remember chr is a pointer to char so *chr is a char */[code]

Then just have additional chars for the the other characters in your stirng1. Maybe you could use an array instead of individual char variables. See Post #8.

Once you have the values in different chars, you should be able to rearrange them.
May 13 '10 #12
jkmyoung
2,057 Expert 2GB
I see. Create 3 char* variables (say tok1, tok2, tok3) to store the 3 segments of the mobile number.
Run strtok each time to get each one, (call it like you are in your while loop). But remember to set the other 3 variables to the pointer at the time. eg:
tok1 = chr;
--
Then use the lengths of each to do the following:
Figure out which token you want first.
Output that.
Figure out which token you want second.
Output that.
Figure out which token you want third.
Output that.
May 13 '10 #13
i have made my own code for this problem but without strcat function that is required in this problem.... can anybody help me how to use strcat... please....bytes experts where are you... not helping me at all..........:-(
May 16 '10 #14
weaknessforcats
9,208 Expert Mod 8TB
strcat works like strcpy.

strcpy copies bytes from the address in the second argument to the address in the first argument until it finds a \0 (binary zero) in the source string. It copies the binary zero then stops.

strcat works the same except it locates the first \0 on or after the address in the first argument and then it copies as does strcpy. For this reason you need to be sure there is a \0 already at the destination location. Either you put one there or you call strcpy for the first part of your result string and then call strcat for the rest of the time.
May 16 '10 #15
above program show the errors

1 :- declaration syntax error
2 :- undefined Symbol 'chr'

please solve the issue. today is my last day for practice.

thanks
May 18 '10 #16
program show the issue...

1:- function should return a value

please solve the problem
May 18 '10 #17
Banfa
9,065 Expert Mod 8TB
I'm sorry we are not fixing someone else's code for you to use.

If you have your own code with your own problems them post them in your own thread giving the code, and the errors you are getting, expected behaviour and actual behaviour if it is a run time error.

declaration syntax error - there was an error in a declaration, the line number should be useful in finding the problem line.

undefined Symbol 'chr' - you have used the symbol chr but it is not defined. Perhaps it is a typo and you meant char or perhaps it is meant to be a variable and you haven't declared it.

function should return a value - a function has a declared return type of anything except void. However when the function returns, either explicitly or implicitly no return value is given.

This thread is now closed!
May 18 '10 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Ian Stanley | last post by:
Hi, Having not done any C programming for a while I am trying to get back into it by converting an old java assignment into C. The problem is I am getting a segmentation fault at runtime which I...
23
by: David Frank | last post by:
How can I write a string function that encloses the input string in quotes "string" ?? below works for the "123 operation but adding " to it clobbers the "123 main() { char...
37
by: Shri | last post by:
hi all, i am writing a code in which i have a char buffer "cwdir" which hold the current working directory by calling the function getcwd(). later i change the directory to "/" as i have to make...
3
by: New | last post by:
I have a String from a file in the form <number> <word>,<word>,<word>,<word> <number> when I try to tokenize it I store it in a char and tokenize it using strtok(string, " ") and then tokenize the...
81
by: Matt | last post by:
I have 2 questions: 1. strlen returns an unsigned (size_t) quantity. Why is an unsigned value more approprate than a signed value? Why is unsighned value less appropriate? 2. Would there...
18
by: intercom5 | last post by:
I'm writing a program in C, and thus have to use C strings. The problem that I am having is I don't know how to reallocate the space for a C string outside the scope of that string. For example:...
9
by: ern | last post by:
I have a <char * Buffer> with the following data: " ; This is a comment preprocess: play music ; comment start:
10
by: zahy[dot]bnaya[At]gmail[dot]com | last post by:
Hi, I am trying to come up with a c style string reverser, I want it to take 1 argument Altough I would never do this in real life. Is there a way to do it? I wrote this function that fails : ...
9
by: Neal Barney | last post by:
I have a C program which runs on a device using a Zilog Z180 microprocessor. While it can address 1MB of RAM, it can only address 64KB at any given time. And of that only 16KB can be used for...
11
by: magicman | last post by:
can anyone point me out to its implementation in C before I roll my own. thx
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.