Hi

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

0092-1234567-333
333-0092-1234567


NOTE: poiters can also be used


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


Thanks

Narue commented: You misspelled www.rentacoder.com. We don't do work for lazy people here at Daniweb. -4
dusktreader commented: do your own homework +0

Recommended Answers

All 11 Replies

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

Dave

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
and
123-0092
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

Yes, I understood the problem - did you look up delimiters and string parsing?

sorry i do not know about this things

delimiters and string parsin ??


i m going to search it in google

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.

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);

str.length() is not working in turbo C or Dev C IDE.

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
                       system("pause"); 
                       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();

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.