C language practice 7.21 per day

Class A

1. (Original) The following program runs as follows: (Note: swap is an exchange in Chinese)

#include <stdio.h>
int swap(int a, int b) {
    int temp;
    temp = a;
    a = b;
    b = temp;
    return a;
}
int main() {
    int a, b;
    a = 23, b = 45;
    swap(a, b);
    printf("a=%d,b=%d\n", a, b);
    return 0;
}

2. (Original) The following program runs as a result:

#include <stdio.h>
int main() {
    int i, j;
    for (i = 0; i < 5; i++) {
        for (j = 0; j < i; j++) {
            putchar(' ');  //A space in quotation marks
        }
        for (j = 0; j < 5; j++) {
            putchar('*');
        }
        putchar('\n');
    }
    return 0;
}

Class B

3. (Volume 1, Question 2, Easy to Change) The functions of the following programs are: the user enters a string, converts all uppercase letters to lowercase, converts lowercase letters to uppercase, and then outputs.Complete the following code to do this.

#include <stdio.h>
void read(char *p) {
    char ch;
    int i = 0;
    while (______________) {  //getchar() is used here
        p[i++] = ch;
    }
    p[i] = '\0';
}
void print(char *p) {
    int i = 0;
    while (______________) {
        putchar(p[i++]);
    }
}
void convert(char *p) {
    ____________________
    //Complete the code here
}
int main() {
    char a[1000];
    read(a);
    convert(a);
    print(a);
    return 0;
}

Program running effect diagram:

 

-------------------------------------------------- The following is the answer---------------------------

 

 

 

 

 

 

 

 

 

1.  a=23,b=45

2.

 

3. 

#include <stdio.h>
void read(char *p) {
    char ch;
    int i = 0;
    while ((ch = getchar()) != '\n') {
        p[i++] = ch;
    }
    p[i] = '\0';
}
void print(char *p) {
    int i = 0;
    while (p[i] != '\0') {
        putchar(p[i++]);
    }
}
void convert(char *p) {
    int i = 0;
    while (p[i] != '\0') {
        if ('a' <= p[i] && p[i] <= 'z') {
            p[i] -= 32;
        } else if ('A' <= p[i] && p[i] <= 'Z') {
            p[i] += 32;
        }
        i++;
    }
}
int main() {
    char a[1000];
    read(a);
    convert(a);
    print(a);
    return 0;
}

 

Posted on Mon, 10 Feb 2020 08:10:53 -0800 by dm3