Thread: How to manipulate a string

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    8

    How to manipulate a string

    I need to input a phone number in the exact form (555) 555-5555 and using the strtok, extract the area code as a token which I have started like this

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void main()
    {
        
         char pNum[15];
         char *tokenPtr;
         printf("Enter a phone number is this format (555) 555-5555: ");
         gets(pNum);
         *tokenPtr = strtok(pNum, " ");
    which extracts the area code, but this is where I get stuck. I need to convert the area code to an integer variable, then extract the first three digits, (save that string), and the last 4. Concatenate the 7 numbers, then convert to a long variable.

    Print the labeled 3 digit area code as the integer(without any other characters). Then print the labeled 7 numbers as a long(without any other characters).

    Can anyone help???

    Code tags added by kermi3

  2. #2
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595

    Code Tags

    In the furture please use Code Tags. They make your code MUCH easier to read and people will be much more likely to help you if you do. And they'll be happy about helping you


    For example:

    Without code tags:

    for(int i=0;i<5;i++)
    {
    cout << "No code tags are bad";
    }

    With Code Tags:
    Code:
    for(int i=0;i<5;i++)
    {
         cout << "This code is easy to read";
    }
    This is of course a basic example...more complicated code is even easier to read with code tags than without.

    I've added code tags for you this time. They can be added by putting [ code ] at the beginning of your code and [ /code ] at the end, only without the spaces. More information on code tags may be found at the link on my signature.
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    8

    Thank you Kermi

    Thank you Kermi!

    I was wondering why my code didn't look the same after I posted the message than the way I had originally typed it.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    95
    this might help a little, it will convert a string to a long, extracting only numbers and leaving everything else out:
    Code:
    long stringtolong(char tempstr[])
    {
      long templong=0;
      int counter;
    
      for(counter=0;tempstr[counter]!='\0';counter++)
        if(tempstr[counter]>='0' && tempstr[counter]<='9')
          templong = templong*10 + tempstr[counter]-'0';
      return templong;
    }
    P.S. if you want the first 3 digits then the next two, then change the function to allow parsing of start/finish points, ie:

    Code:
    long firstchars;
    long restchars;
    
    long stringtolong(char tempstr[],int start, int end)
    {
      long templong=0;
      int counter;
    
      for(counter=start;counter<=end;counter++)
        if(tempstr[counter]>='0' && tempstr[counter]<='9')
          templong = templong*10 + tempstr[counter]-'0';
      return templong;
    }
    
    firstchars = stringtoolong(phonenumber,0,2);
    restchars = stringtoolong(phonenumber,3,strlen(phonenumber));
    the firstchars should have only the first 3 numbers and the restchars should have all the rest.

    Hope this helps

  5. #5
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    Thank you Kermi!

    I was wondering why my code didn't look the same after I posted the message than the way I had originally typed it.
    You're quite welcome. Any other questions please feel free to PM me.
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. string manipulate
    By thinice16 in forum C Programming
    Replies: 3
    Last Post: 08-01-2008, 07:30 PM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM