Thread: Help with parsing in C

  1. #1
    Registered User
    Join Date
    Apr 2014
    Posts
    50

    Help with parsing in C

    You should implement the following function:

    int is_phone_number(char* string)
    This function will take in a string and return 1 if it looks like a phone number 0 otherwise. A phone number takes the form “(xxx)-xxx-xxxx” where the x’s are digits 0-9. So for example (123)-456-7890 is a valid phone number while 123 456-7890 is not.

    You should also write a main function that parses a text document and prints out all of the phone numbers found. Hint, look up the strtok function.


    Sample input:
    Please call me at (123)-456-789 sometime tonight.

    Sample output:
    Phone number : (123)-456-7890

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    Announcements - General Programming Boards
    You've already been given a hint, so try it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2014
    Posts
    50
    Quote Originally Posted by Salem View Post
    Announcements - General Programming Boards
    You've already been given a hint, so try it.

    Code:
    #include <stdio.h> 
    #include <string.h> 
    
    char mask [] = "(999)999-9999"; 
    
    int is_phone_number (char* string) { 
    int i = 0; 
    int result = 1; 
    while (string[i]>0) { 
    char c = string [i]; 
    if (mask[i]=='9') { 
    if (c<'0' || c>'9') { 
    result = 0; 
    break; 
    }	
    } 
    else if (c!=mask[i]) { 
    break; 
    result = 0; 
    } 
    i++; 
    }	
    return result; 
    } 
    
    int main() { 
    int is = is_phone_number ("(301)555-1234"); 
    printf ("\n%d", is); 
    return 0; 
    }
    
    



    any input on this code?

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    120
    1. Make up your mind on the format of a valid phone number. In the post you provided (xxx)-xxx-xxxx, but in the code you are missing a dash.
    2. Your function will return 1 if the input string is empty - is that what you intended?

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by DRK View Post
    2. Your function will return 1 if the input string is empty - is that what you intended?
    A naked ADSL connection?
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    Jun 2009
    Posts
    120
    Quote Originally Posted by grumpy View Post
    A naked ADSL connection?
    How is your question related to my post?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. XML parsing with C++
    By jachstws7 in forum C++ Programming
    Replies: 9
    Last Post: 06-23-2012, 12:22 PM
  2. xml parsing in c
    By er.rohan88 in forum C Programming
    Replies: 5
    Last Post: 01-21-2010, 06:39 AM
  3. CSV Parsing
    By spadez in forum C++ Programming
    Replies: 13
    Last Post: 02-04-2009, 10:28 AM
  4. String parsing(parsing comments out of HTML file)
    By slcjoey in forum C# Programming
    Replies: 0
    Last Post: 07-29-2006, 08:28 PM
  5. Parsing XML
    By CompiledMonkey in forum C# Programming
    Replies: 2
    Last Post: 05-03-2002, 07:08 AM