Switch

Exercise 1

Write a below source code and save it as "switch1.c". After compilation, confirm how "switch" works.

#include <stdio.h>

int main(void) {

    int  a;

    for(a=1; a<=5; a++){
        printf("-------------\n");
        switch(a){
            case 1:
                printf("a=1\n");
            case 3:
                printf("a=3\n");
            case 5:
                printf("a=5\n");
            default :
                printf("others\n");
        }
    }
    
    return (0);
}

Exercise 2

Write a below source code and save it as "switch2.c". After compilation, confirm how "break" works in switch-statement.

#include <stdio.h>

int main(void) {

    int  a;

    for(a=1; a<=5; a++){
        printf("-------------\n");
        switch(a){
            case 1:
                printf("a=1\n");
                break;
            case 3:
                printf("a=3\n");
                break;
            case 5:
                printf("a=5\n");
                break;
            default :
                printf("others\n");
        }
    }

    return (0);
}

Exercise 3

Answer whether it is possible to replace line (I) of a following source code with expressions (1) to (5) listed in below.

  1. case 'A':
  2. case 3.5:
  3. case "ABC":
  4. case a>2:
  5. case MAX:
#include <stdio.h>
#define MAX 2

int main(void){

    int a;

    for(a=0; a<5; a++){
        printf("a = %d \n", a);
        switch(a){
            case 3:        <-- (I)
                printf("Replacement is successful\n");
                break;
        }
    }
    
    return (0);
}

Excercise 4

Answer following questions without typing source code and executing it. After you answer, confirm whether your answer is correct by executing it.

  1. Answer execution result shown on screen by executing following source code.
  2. Write expression related to "switch case" of following source code using "if-statement".
#include <stdio.h>

int main(void){

    char a[]="I am very hungry";
    int i=0;

    printf("String = %s\n", a);
    while(a[i] != '\0'){
        switch(a[i]){
            case 'a':
                a[i] = 'b';
                break;
            case 'v':
            case 'h':
                a[i] = 'w';
                break;
            case 'y':
                a[i] = 'z';
                break;
            default:
                a[i] = '*';
        }
        i++;
    }
    printf("Number of characters = %d\n", i);
    printf("String = %s\n", a);

    return (0);
}

Excercise 5

We want to show classroom in accordance with class, as shown in following table. Even though lowercase, e.g. a, b or c, is input, message should be shown same as uppercase, e.g., A, B or C is input. Program to meet this requirement and save such source code as "switch5.c". Confirm whether it can execute properly.

Class Classroom
A 101
B 201
C 202
(Execution result)
$ 
$  ./a.out
Input your class (A, B or C)  A
Classroom is 101
$
$  ./a.out
Input your class (A, B or C)  a
Classroom is 101
$
$  ./a.out
Input your class (A, B or C)  B
Classroom is 201
$
$  ./a.out
Input your class (A, B or C)  c
Classroom is 202
$
$  ./a.out
Input your class (A, B or C)  r
No class. Input correct class
$

Exercise 6

We want to show arbitrary number of figures in different styles, which can be determined by input option, as shown in following table. In this exercise, single option is assumed to be selected. Program and save such source code as "swithc6.c". Confirm whether it can execute properly.

Option Meaning
a Display each figure line-by-line
b Diplay figure with element order (data[0]=??, data[1]=??, ...)
c Diplay reversely
(Execution result)
$
$ ./a.out
How many number of figures do you input?
5
Input 5 number of figures
4 5 6 7 8
Select option for display
a: Display each figure line-by-line
b: Diplay figure with element order
c: Diplay reversely
Input 'z' without option
a
4
5
6
7
8

$
$ ./a.out
How many number of figures do you input?
5
Input 5 number of figures
4 5 6 7 8
Select option for display
a: Display each figure line-by-line
b: Diplay figure with element order
c: Diplay reversely
Input 'z' without option
b
data[0]=4 data[1]=5 data[2]=6 data[3]=7 data[4]=8 

$
$ ./a.out
How many number of figures do you input?
5
Input 5 number of figures
4 5 6 7 8
Select option for display
a: Display each figure line-by-line
b: Diplay figure with element order
c: Diplay reversely
Input 'z' without option
c
8 7 6 5 4 

$
$ ./a.out
How many number of figures do you input?
5
Input 5 number of figures
4 5 6 7 8
Select option for display
a: Display each figure line-by-line
b: Diplay figure with element order
c: Diplay reversely
Input 'z' without option
z
4 5 6 7 8 

$
$ ./a.out
How many number of figures do you input?
5
Input 5 number of figures
4 5 6 7 8
Select option for display
a: Display each figure line-by-line
b: Diplay figure with element order
c: Diplay reversely
Input 'z' without option
d
4 5 6 7 8 

$

Exercise 7

In addition to Exercise 6, we want to show arbitrary number of figures in different styles, and multi-option is allowed to be selected. Program and save such source code as "swithc7.c". Confirm whether it can execute properly.

Caution: In both cases of options "ab" and "ba", result shown on the screen should be same.

(Execution result)
$
$ ./a.out
How many number of figures do you input?
5
Input 5 number of figures
4 5 6 7 8
Select option for display (multi-selection is possible)
a: Display each figure line-by-line
b: Diplay figure with element order
c: Diplay reversely
Input 'z' without option
ab
data[0]=4
data[1]=5
data[2]=6
data[3]=7
data[4]=8

$
$ ./a.out
How many number of figures do you input?
5
Input 5 number of figures
4 5 6 7 8
Select option for display (multi-selection is possible)
a: Display each figure line-by-line
b: Diplay figure with element order
c: Diplay reversely
Input 'z' without option
ba
data[0]=4
data[1]=5
data[2]=6
data[3]=7
data[4]=8

$
$ ./a.out
How many number of figures do you input?
5
Input 5 number of figures
4 5 6 7 8
Select option for display (multi-selection is possible)
a: Display each figure line-by-line
b: Diplay figure with element order
c: Diplay reversely
Input 'z' without option
bc
data[4]=8 data[3]=7 data[2]=6 data[1]=5 data[0]=4 

$
$ ./a.out
How many number of figures do you input?
5
Input 5 number of figures
4 5 6 7 8
Select option for display (multi-selection is possible)
a: Display each figure line-by-line
b: Diplay figure with element order
c: Diplay reversely
Input 'z' without option
ca
8
7
6
5
4

$
$ ./a.out
How many number of figures do you input?
5
Input 5 number of figures
4 5 6 7 8
Select option for display (multi-selection is possible)
a: Display each figure line-by-line
b: Diplay figure with element order
c: Diplay reversely
Input 'z' without option
bac
data[4]=8
data[3]=7
data[2]=6
data[1]=5
data[0]=4

$
$ ./a.out
How many number of figures do you input?
5
Input 5 number of figures
4 5 6 7 8
Select option for display (multi-selection is possible)
a: Display each figure line-by-line
b: Diplay figure with element order
c: Diplay reversely
Input 'z' without option
cab
data[4]=8
data[3]=7
data[2]=6
data[1]=5
data[0]=4

$
$ ./a.out
How many number of figures do you input?
5
Input 5 number of figures
4 5 6 7 8
Select option for display (multi-selection is possible)
a: Display each figure line-by-line
b: Diplay figure with element order
c: Diplay reversely
Input 'z' without option
4 5 6 7 8 
$

Lessons for C programming
Junichi Susaki, Kyoto University