Solved

switch statement

Posted on 1997-03-07
1
656 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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now