C++ string use strtok, strlen, and strcat

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mrshafiullah
    New Member
    • May 2010
    • 2

    #1

    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
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    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.

    Comment

    • silverhawk2011
      New Member
      • May 2010
      • 7

      #3
      give me solution ;;;;;;;;;;;;;;; ;;;.....

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        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.

        Comment

        • silverhawk2011
          New Member
          • May 2010
          • 7

          #5
          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??

          Comment

          • jkmyoung
            Recognized Expert Top Contributor
            • Mar 2006
            • 2057

            #6
            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.

            Comment

            • silverhawk2011
              New Member
              • May 2010
              • 7

              #7
              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!

              Comment

              • jkmyoung
                Recognized Expert Top Contributor
                • Mar 2006
                • 2057

                #8
                output token[1]; //2nd token, 1st position
                output token[2]; //3rd token, 2nd position
                output token[0]; //1st token, 3rd position.

                Comment

                • silverhawk2011
                  New Member
                  • May 2010
                  • 7

                  #9
                  young im not getting the way you explaining....g ive some refferrence in c++ according the give problem

                  Comment

                  • weaknessforcats
                    Recognized Expert Expert
                    • Mar 2007
                    • 9214

                    #10
                    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.

                    Comment

                    • silverhawk2011
                      New Member
                      • May 2010
                      • 7

                      #11
                      #include<iostre am.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???

                      Comment

                      • weaknessforcats
                        Recognized Expert Expert
                        • Mar 2007
                        • 9214

                        #12
                        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.

                        Comment

                        • jkmyoung
                          Recognized Expert Top Contributor
                          • Mar 2006
                          • 2057

                          #13
                          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.

                          Comment

                          • silverhawk2011
                            New Member
                            • May 2010
                            • 7

                            #14
                            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..........:-(

                            Comment

                            • weaknessforcats
                              Recognized Expert Expert
                              • Mar 2007
                              • 9214

                              #15
                              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.

                              Comment

                              Working...