Solved

switch statement

Posted on 1997-03-07
1
661 Views
Last Modified: 2006-11-17
* 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
Comment
Question by:hileen
1 Comment
 

Accepted Solution

by:
james bailey earned 70 total points
ID: 1249692
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question