I need code of how to copy part of string.

like 0092-333-1234567

so i need to copy the part 333 or 1234567 in to a new string variable

sample inputs


NOTE: poiters can also be used

i need it in Turbo C++ or Dev C++


Edited 6 Years Ago by Bukhari1986: n/a

do your own homework
You misspelled www.rentacoder.com. We don't do work for lazy people here at Daniweb.

What have you tried? Lookup "delimiters". Also, unless I'm mistaken, Turbo c++ and Dev c++ are simple IDEs, not languages, right?


i just need to copy the string after the character "-"

i.e. if i enter 0092-123-1234567

or 1234567-123-0092

then i need to copy the string 123-1234567
from both the strings to a new pointer or string

with turbo C and Dev C i meant that in simple C. not visual C ++ or any thign else

sorry i do not know about this things

delimiters and string parsin ??

i m going to search it in google

Edited 6 Years Ago by Bukhari1986: n/a

I can't do it correctly.

when i try with pointers, the work is done. but the IDE crashes after that. and i have to compile and run it from the start. :(

Acutally i want to copy each part of the three from the Number 0092-123-1234567
into a separate variable. After that i am doing further process on it.

I copy the first part through strtok() function. for the second part i use for loop to copy it to another pointer. it works but crashes after output is complete.

if anybody knows any function which will copy the second part of the string to a variable, then kindly share it.

I knew such function once. but now i am not remembering it. i searched it a lot.

The function i knew was copying first part and second part of the string to two different variables dividing it on the first occurance of any specified character e.g. a space or anything you mention.

Edited 6 Years Ago by Bukhari1986: n/a

You should look up the string class method substr, it does what you need.

string str="0092-123-1234567";
int cpos=0;
while (cpos<str.length() && str[cpos]!='-')cpos++;
if (cpos>=str.length())error(); //no '-' found
string tail=str.substr(cpos+1,str.length()-cpos-1);

Edited 6 Years Ago by Aranarth: n/a

Please be more clear. How have you defined str? Did you #include <string> ? Did you use std::string? What do you mean by "not working"?

yes i hve defined everything mentioned.

it gives error the the function is not defined.

i will be back with more details soon. now i am in hurry.

Just post your code, already. Ideally write up a bare bones program that exhibits your problem without being excessively long. Playing twenty questions is unproductive, especially when the problem sounds like something most of us can solve at a glance.

char input[20];  //character array for taking input from user 

char cntryCode[4], cityCode[3], number[7], phNum[20];

char *token= NULL;

cout << "Enter the complete phone number : "; //taking complete phone number from user
cin >> input;

//If length of input is not 16, it means user entered less than or more than required characters
      if(strlen(input) != 16) 
                       cout << "Invalid input format!" << endl; //displaying error message
                       exit(1);    // exit from the program after displaying the error message

token = strtok(input, "-"); //token pointer contains the first token before "-"

while(token != NULL)  // Loop will continue until the value of token is NULL

     if(strlen(token) == 3) //if length of token is 3, it means it is city code
        strcpy(cityCode, token); // copying value in cityCode character array
     if(strlen(token) == 4)  //if length of token is 4, it means it is country code
        strcpy(cntryCode, token); // copying the value in cntryCode character array
     if(strlen(token) == 7)  // if length of token is 7, it means it is 7-digit number
        strcpy(number, token);  // copying the value in number array
   token = strtok(NULL, "-");  // assigning the next value after token "-" to token pointer

problem solved.

I was unable to figure out the use of NULL in the function strtok();

This question has already been answered. Start a new discussion instead.