Thread: Array-Switch-Default

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    29

    Array-Switch-Default

    1) Prompting the user to enter a phone number.
    2) Read the characters, if its alphabets and lower case, change it to upper case. if not, leave it.
    3) Read and store the characters in the first array O[i]
    4) for statement for i
    5) Switch-case loop. Here, I am trying to convert the entered alphabets into phone numbers and save them in a second array. However, if the input character was not an alphabet (none of the cases) i'm setting a default case to keep the same character but save it in the second array.
    6) Print the 2nd array.

    My outcome should be:
    Enter Phone Number: 1-800-COL-LecT
    Numeric Form: 1-800-265-5328

    Code:
    #include <stdio.h> 
    #include <ctype.h>
     
     
    int main()
    {
        #define N 15
    
    
        char O[N];
        char S[N];
        char ch; 
        int i=0;
        
        printf("Enter phone number: ");
    
    
        while ( (ch=toupper(getchar()) ) != '\n' && i<N)
        {
            O[i] = ch;
            i++;
        }
        printf("Numeric Form      : ");
         
        for(i=0;i<N;i++)
        {
            
            switch (O[i])
            {
                
                case '.' :
                case ',' :
                case '@' :               
                    S[i] = '1';
                    break;
                case 'A' :
                case 'B' :
                case 'C' :
                    S[i] = '2';
                    break;
                case 'D' :
                case 'E' :
                case 'F' :
                    S[i] = '3';
                    break;
                case 'G' :
                case 'H' :
                case 'I' :
                    S[i] = '4';
                    break;
                case 'J' :
                case 'K' :
                case 'L' :
                    S[i] = '5';
                    break;
                case 'M' : 
                case 'N' :
                case 'O' :
                    S[i] = '6';
                    break;
                case 'P' :
                case 'Q' :
                case 'R' :
                case 'S' :
                    S[i] = '7';
                    break;
                case 'T' :
                case 'U' :
                case 'V' :
                    S[i] = '8';
                    break;
                case 'W' :
                case 'X' :
                case 'Y' :
                case 'Z' :
                    S[i] = '9';
                    break;
                default:
                  O[i] = S[i];
                     
    
    
                    printf("%c", S[i]);
            }
             
            i++;
           }
     
    return(0);
    }
    Last edited by Gkendoyan; 10-24-2014 at 09:24 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C: Default case in switch not working
    By LunaLux in forum C Programming
    Replies: 5
    Last Post: 04-24-2011, 08:46 AM
  2. switch () {default: <something>; case ...
    By Kennedy in forum C Programming
    Replies: 4
    Last Post: 11-30-2010, 12:40 PM
  3. Switch statement / default:
    By kcpilot in forum C Programming
    Replies: 4
    Last Post: 12-02-2008, 03:14 PM
  4. default switch statement not working
    By gL_nEwB in forum C++ Programming
    Replies: 3
    Last Post: 05-13-2006, 10:13 AM
  5. Returning from function - switch selects default
    By rkooij in forum C Programming
    Replies: 24
    Last Post: 03-17-2006, 07:27 AM