?
Solved

switch statement

Posted on 1997-03-07
1
Medium Priority
?
665 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 

Accepted Solution

by:
james bailey earned 210 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand opening and reading files 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.
Suggested Courses
Course of the Month11 days, 9 hours left to enroll

752 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