• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 670
  • Last Modified:

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.
0
hileen
Asked:
hileen
1 Solution
 
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now