• C

switch statement

* Write a C program using the switch statement to convert user inputted text to the telephone number equivalent. If the user input has numbers in it then print that number, if the text has a z or a q then print an invalid message.
Sample input:
1 800 BUY THIS
Output
1800 289 8447
**I can't find any ideas to start writing this program.
hileenAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

james baileyCommented:
Hi hileen,
Here is some code to get you started:

#include <stdlib.h>
#include <ctype.h>

void convert(char *input, char *output) {
  int index = 0;
  while (input[index] != '\0') {
    switch (toupper(input[index])) {
      case 'A': case 'B': case 'C': {
        output[index] = '2';
        break;
      }                
      case 'D': case 'E': case 'F': {
        output[index] = '3';
        break;
      }                
      case 'G': case 'H': case 'I': {
        output[index] = '4';
        break;
      }                
      /* follow the above pattern for the rest */
      case 'Q': case 'Z': {
        printf("Error.\n");
        output[0] = '\0';
        return;
        break;
      }
      default: {
        output[index] = input[index];
        break;        
      }
    }
    index++;
  }  
  output[index] = '\0';
}


This function will take a string for input and will put the converted string into the buffer passed in as the second parameter. It begins with the first character in the input and looks at it.  The switch statement will jump down to where the match is for that particular character.  For example, if the first character in the input is 'A', then after the switch statement, it will go to where "case 'A':" is located and execute the code there until it hits the "break" statement.  Since it continues til it hits the break, "case 'B':" can be put in after the "case 'A':" and it will go there if a 'B' is the character that is switched.  
 
When it performs the switch, the function "toupper" is called.  This function will take the character and convert it to upper case.  This is important because the switch won't match 'a' to "case 'A':".  To get this function, #include <ctype.h> and #include <stdlib.h> at the top of the file.

After the switch matches up what character is being converted, it simply puts the converted value into the output string.  For example, supposed the current "index" is 4 and input[4] is 'D', then it will go to where "case 'D':" is and execute "output[index] = '3';".  This will put a 3 in the output where the D was in the input.  For the two error conditions, Q and Z, the code above will print "Error." and exit the function.  

The "default:" statement in the switch block will catch everything that doesn't get caught in the other cases.  So, if a 'B' is switched, it will get caught where "case 'B':" is at.  If a number is switched, nothing else will catch it above, so it will go to the default section.  The code above simply copies the character that didn't match anything else into the output buffer.  This way, if the input has numbers or punctuation in it, it will just copy those characters into the output.        

The code will loop until it runs into the null character that terminates the input string.  Each time it loops it increases the index by one and examines the next character in the input.  After it has found the null character, it will stop looping, and it is important to make sure and put the null character into the output string so that the output ends where it should.  This is what "output[index] = '\0';" does at the end of the function.

Hope this helps you get started with what you need to do, good luck with it.

James Bailey


0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.