Thread: Parsing a Phone number-strings

  1. #1
    Registered User
    Join Date
    Jan 2013
    Location
    San Jose, CA
    Posts
    53

    Exclamation Parsing a Phone number-strings

    Hey everyone,
    I'm stuck on a relatively easy homework:

    "Write a program to parse userís input of a telephone number. Your program should accept a string in any of the following format:

    1. 4147778888
    2. (414)7778888
    3. (414)777-8888


    If the user does not input a number using one of the formats, the program will display an error message to the user and ask the user to try again. If the user has entered a valid format, the program will display a string that contains only digits in the user input. For examples, the program would display 4147778888."

    so far I have this:
    Code:
    #include <stdio.h>
    
    int main (void)
    {
    
    char str[13];
    
    printf("Welcome\n");
    printf("Please enter a phone number in any of the following format:\n");
    printf("aaabbbbbbb, (aaa)bbbbbbb, (aaa)bbb-bbbb\n");
    
    scanf("%10[^()-]", str);
    printf("Here is your phone number formatted\n");
    printf("in the following way: aaabbbbbbb\n");
    printf("%s", str);
    
    return 0;
    }
    Does anyone know what I'm doing wrong or if I'm on the right track at all? I started this homework late, it's due in 2 days, and I'm going crazy

    Thank You!

  2. #2
    Registered User
    Join Date
    Feb 2013
    Location
    Buea Cameroon
    Posts
    64
    This fuction has a poblem
    Code:
    scanf("%10[^()-]", str); //you surely forgot to add %s
    Since the program asks for another number if the input is wrong, you will need a loop to keep asking for the number if a wrong number is entered
    A condition should also check if the number entered is correct after each call of "scanf"
    A way to check this is
    Code:
    //This is just an algorithm
    
    while(input != correct)
      if str[0] == '(' //then
        if str[4] != ) //then the input is wrong
        //you can also count the digits bettween the () brackets if it is exactly = 3 using the isdigit() function in ctype.h
        //If total digits in str == 13 then str[8] == '-' if not, input is wrong
        //you can also count the total digits if it is exactly = 7  using the isdigit() function in ctype.h

  3. #3
    Registered User
    Join Date
    Jan 2013
    Location
    San Jose, CA
    Posts
    53

    Exclamation

    Ohh, okey, I undestand how to check if the input is correct or not now, thank you!!
    I wrote some fast code just now, but without checking if the input is correct. I plainly enter a number, for example, (408)999-3333
    and the output is this: 4408999333
    It ignores all the parentheses and hyphens, but doesn't print the output correctly somehow...I can't find the problem, is it something in my loops??

    here's the code:
    Code:
    #include <stdio.h>
    
    /* forward declaration / prototype */
    void removeAll( char s[], char x );
    
    int main ()
    {
        char s[50];
        printf("Enter your phone number:\n");
        scanf("%s", s);
        printf( "Your phone number: %.13s\n", s );
    
        int i, j;
        for( i=0; s[i]!=0; ++i )
        {
            while(s[i]=='(')
            {
                {
                    {
                        j=i;
                        {
                            s[j]=s[j+1];
                            ++j;
                        }
                    }
                }
            }
        }
        for( i=0; s[i]!=0; ++i )
        {
            while(s[i]==')')
            {
                j=i;
                while(s[j]!=0)
                {
                    s[j]=s[j+1];
                    ++j;
                }
            }
        }
    
        removeAll( s, '-' );
        printf( "Formatted phone number: %.10s\n", s );
    
    
        getchar();
        return 0;
    }
    
    void removeAll( char s[], char x )
    {
        int i, j;
        for( i=0; s[i]!=0; ++i )
        {
            while(s[i]==x)
            {
                j=i;
                while(s[j]!=0)
                {
                    s[j]=s[j+1];
                    ++j;
                }
            }
        }
    }
    Thank You for helping me out! I greatly appreciate it.

  4. #4
    Registered User
    Join Date
    Jan 2013
    Location
    San Jose, CA
    Posts
    53
    OMG......I JUST DID IT!!!! IT PRINTS OUT THE CORRECT FORMAT!!!!!!! AHH I"M EXTREMELY HAPPY RIGHT NOW OMG. Okey, now I have to work on the loops for checking if the input is correct or not. I will post if I can't get it to work.

    Thank You again so much!!!

    and here's my working code:
    Code:
    #include <stdio.h>
    
    /* forward declaration / prototype */
    void remove_hyphen( char s[], char x );
    void remove_left_paren( char s[], char f );
    void remove_right_paren( char s[], char g );
    
    int main ()
    {
        char s[50];
        printf("Enter your phone number:\n");
        scanf("%s", s);
        printf( "Your phone number: %.13s\n", s );
    
        remove_hyphen( s, '-' );
        remove_left_paren(s, '(');
        remove_right_paren(s, ')');
        printf( "Formatted phone number: %.10s\n", s );
    
    
        getchar();
        return 0;
    }
    
    void remove_hyphen( char s[], char x )
    {
        int i, j;
        for( i=0; s[i]!=0; ++i )
        {
            while(s[i]==x)
            {
                j=i;
                while(s[j]!=0)
                {
                    s[j]=s[j+1];
                    ++j;
                }
            }
        }
    }
    
    void remove_left_paren( char s[], char f )
    {
        int i, j;
        for( i=0; s[i]!=0; ++i )
        {
            while(s[i]==f)
            {
                j=i;
                while(s[j]!=0)
                {
                    s[j]=s[j+1];
                    ++j;
                }
            }
        }
    }
    
    void remove_right_paren( char s[], char g )
    {
        int i, j;
        for( i=0; s[i]!=0; ++i )
        {
            while(s[i]==g)
            {
                j=i;
                while(s[j]!=0)
                {
                    s[j]=s[j+1];
                    ++j;
                }
            }
        }
    }

  5. #5
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    I haven't checked if they work as advertised, but if you are just removing all parenthesis' and any hyphens from the input, you are not following the directions correctly. You are to not accept a string of characters that do not form to the acceptable formats listed. One solution would be to read the char's one at a time. If the first one is a number, you should see nothing but numbers from then on. If the first char is a left parenthesis, then check to see the rest forms to an acceptable format. Else quit the program or force the user to do a "re-entry".
    Last edited by jwroblewski44; 06-16-2013 at 10:55 PM.
    "Simplicity is the ultimate sophistication." - Leonardo da Vinci

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,975
    You should have
    Code:
        remove_char( s, '-' );
        remove_char(s, '(');
        remove_char(s, ')');
    and only ONE copy of the code (not three, with only a change of variable name)
    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.

  7. #7
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by acho.arnold View Post
    This fuction has a poblem
    Code:
    scanf("%10[^()-]", str); //you surely forgot to add %s
    No, that line is syntactically correct. %[ is a legal format specifier. See scanf, fscanf, sscanf - cppreference.com.

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Where's my phone number going?
    By Dino in forum C Programming
    Replies: 1
    Last Post: 01-16-2010, 08:33 PM
  2. strtok() a phone number
    By Sure in forum C Programming
    Replies: 3
    Last Post: 06-27-2005, 06:46 PM
  3. Validating 'phone number
    By explosive in forum C++ Programming
    Replies: 9
    Last Post: 04-01-2005, 03:22 AM
  4. registering a phone number
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 07-08-2004, 10:15 PM
  5. Phone number
    By beachchutney in forum C++ Programming
    Replies: 1
    Last Post: 05-03-2002, 04:18 PM

Tags for this Thread