MindMap Gallery C Language Question 11-15
This is a mind map about questions 11-15 of C language, and the main contents include: 15. C language examples - judge vowel consonants, 14. C language examples - odd and even numbers within the loop interval range, 13. C language Example - judge odd and even numbers, 12. C language instance - exchange the values of two numbers, 11. C language instance - calculate the size of int float double and char bytes.
Edited at 2025-02-13 20:34:48Rumi: 10 dimensions of spiritual awakening. When you stop looking for yourself, you will find the entire universe because what you are looking for is also looking for you. Anything you do persevere every day can open a door to the depths of your spirit. In silence, I slipped into the secret realm, and I enjoyed everything to observe the magic around me, and didn't make any noise. Why do you like to crawl when you are born with wings? The soul has its own ears and can hear things that the mind cannot understand. Seek inward for the answer to everything, everything in the universe is in you. Lovers do not end up meeting somewhere, and there is no parting in this world. A wound is where light enters your heart.
Chronic heart failure is not just a problem of the speed of heart rate! It is caused by the decrease in myocardial contraction and diastolic function, which leads to insufficient cardiac output, which in turn causes congestion in the pulmonary circulation and congestion in the systemic circulation. From causes, inducement to compensation mechanisms, the pathophysiological processes of heart failure are complex and diverse. By controlling edema, reducing the heart's front and afterload, improving cardiac comfort function, and preventing and treating basic causes, we can effectively respond to this challenge. Only by understanding the mechanisms and clinical manifestations of heart failure and mastering prevention and treatment strategies can we better protect heart health.
Ischemia-reperfusion injury is a phenomenon that cellular function and metabolic disorders and structural damage will worsen after organs or tissues restore blood supply. Its main mechanisms include increased free radical generation, calcium overload, and the role of microvascular and leukocytes. The heart and brain are common damaged organs, manifested as changes in myocardial metabolism and ultrastructural changes, decreased cardiac function, etc. Prevention and control measures include removing free radicals, reducing calcium overload, improving metabolism and controlling reperfusion conditions, such as low sodium, low temperature, low pressure, etc. Understanding these mechanisms can help develop effective treatment options and alleviate ischemic injury.
Rumi: 10 dimensions of spiritual awakening. When you stop looking for yourself, you will find the entire universe because what you are looking for is also looking for you. Anything you do persevere every day can open a door to the depths of your spirit. In silence, I slipped into the secret realm, and I enjoyed everything to observe the magic around me, and didn't make any noise. Why do you like to crawl when you are born with wings? The soul has its own ears and can hear things that the mind cannot understand. Seek inward for the answer to everything, everything in the universe is in you. Lovers do not end up meeting somewhere, and there is no parting in this world. A wound is where light enters your heart.
Chronic heart failure is not just a problem of the speed of heart rate! It is caused by the decrease in myocardial contraction and diastolic function, which leads to insufficient cardiac output, which in turn causes congestion in the pulmonary circulation and congestion in the systemic circulation. From causes, inducement to compensation mechanisms, the pathophysiological processes of heart failure are complex and diverse. By controlling edema, reducing the heart's front and afterload, improving cardiac comfort function, and preventing and treating basic causes, we can effectively respond to this challenge. Only by understanding the mechanisms and clinical manifestations of heart failure and mastering prevention and treatment strategies can we better protect heart health.
Ischemia-reperfusion injury is a phenomenon that cellular function and metabolic disorders and structural damage will worsen after organs or tissues restore blood supply. Its main mechanisms include increased free radical generation, calcium overload, and the role of microvascular and leukocytes. The heart and brain are common damaged organs, manifested as changes in myocardial metabolism and ultrastructural changes, decreased cardiac function, etc. Prevention and control measures include removing free radicals, reducing calcium overload, improving metabolism and controlling reperfusion conditions, such as low sodium, low temperature, low pressure, etc. Understanding these mechanisms can help develop effective treatment options and alleviate ischemic injury.
11-15
11. C Language Example - Calculate the size of int float double and char bytes
1. Use the sizeof operator directly: This is the most direct and commonly used method.
#include <stdio.h>
int main() {
printf("Size of int: %zu bytes ", sizeof(int));
//%zu is the format specifier used to print variables of type size_t.
printf("Size of float: %zu bytes ", sizeof(float));
printf("Size of double: %zu bytes ", sizeof(double));
printf("Size of char: %zu bytes ", sizeof(char));
return 0;
}
2. Use the sizeof operator to calculate the byte size of a variable: This method is the same as directly calculating the data type, but operates on the variable.
#include <stdio.h>
int main() {
int intVar;
float float floatVar;
double doubleVar;
char charVar;
printf("Size of intVar: %zu bytes ", sizeof(intVar));
printf("Size of floatVar: %zu bytes ", sizeof(floatVar));
printf("Size of doubleVar: %zu bytes ", sizeof(doubleVar));
printf("Size of charVar: %zu bytes ", sizeof(charVar));
return 0;
}
3. Use the sizeof operator to calculate the byte size of an array: It can be used to calculate the size of an array type.
#include <stdio.h>
int main() {
int intArray[5];
float float floatArray[5];
double doubleArray[5];
char charArray[5];
printf("Size of intArray: %zu bytes ", sizeof(intArray));
printf("Size of floatArray: %zu bytes ", sizeof(floatArray));
printf("Size of doubleArray: %zu bytes ", sizeof(doubleArray));
printf("Size of charArray: %zu bytes ", sizeof(charArray));
// Calculate the size of a single element in the array
printf("Size of one int in intArray: %zu bytes ", sizeof(intArray) / sizeof(intArray[0]));
printf("Size of one float in floatArray: %zu bytes ", sizeof(floatArray) / sizeof(floatArray[0]));
printf("Size of one double in doubleArray: %zu bytes ", sizeof(doubleArray) / sizeof(doubleArray[0]));
printf("Size of one char in charArray: %zu bytes ", sizeof(charArray) / sizeof(charArray[0]));
return 0;
}
4. Use the sizeof operator to calculate the byte size of a structure: It can be used to calculate the size of the structure type.
#include <stdio.h>
struct Example {
int anInt;
float aFloat;
double aDouble;
char aChar;
};
int main() {
printf("Size of struct Example: %zu bytes ", sizeof(struct Example));
return 0;
}
12. C Language Example - Exchange the values of two numbers
1. Method 1: Use temporary variables This is the most common and easiest way to use an extra temporary variable to store the value of one of the numbers for use during exchange.
#include <stdio.h>
int main() {
int a, b, temp;
printf("Please enter the first integer: ");
scanf("%d", &a);
printf("Please enter the second integer: ");
scanf("%d", &b);
printf("Before exchange: a = %d, b = %d ", a, b);
temp = a;
a = b;
b = temp;
printf("After exchange: a = %d, b = %d ", a, b);
return 0;
}
2. Method 2: No temporary variables (using addition and subtraction) This method uses addition and subtraction to exchange the values of two numbers without additional temporary variables.
#include <stdio.h>
int main() {
int a, b;
printf("Please enter the first integer: ");
scanf("%d", &a);
printf("Please enter the second integer: ");
scanf("%d", &b);
printf("Before exchange: a = %d, b = %d ", a, b);
a = a b;
b = a - b;
a = a - b;
printf("After exchange: a = %d, b = %d ", a, b);
return 0;
}
3. Method 3: Do not use temporary variables (using multiplication and division) This method uses multiplication and division to exchange the values of two numbers. It should be noted that this approach can cause overflow problems, especially when dealing with large integers.
#include <stdio.h>
int main() {
int a, b;
printf("Please enter the first integer: ");
scanf("%d", &a);
printf("Please enter the second integer: ");
scanf("%d", &b);
printf("Before exchange: a = %d, b = %d ", a, b);
a = a * b;
b = a / b;
a = a / b;
printf("After exchange: a = %d, b = %d ", a, b);
return 0;
}
4. Method 4: Do not use temporary variables (using XOR operation) This method uses the exclusive OR operation in bit operations to exchange the values of two numbers without additional temporary variables. The advantage of this approach is that there is no need for additional storage space and may improve performance in some cases.
#include <stdio.h>
int main() {
int a, b;
printf("Please enter the first integer: ");
scanf("%d", &a);
printf("Please enter the second integer: ");
scanf("%d", &b);
printf("Before exchange: a = %d, b = %d ", a, b);
a = a ^ b;
b = a ^ b;
a = a ^ b;
printf("After exchange: a = %d, b = %d ", a, b);
return 0;
}
5. Method 5: Use pointers This method uses pointers to exchange the values of two numbers. By passing the address of a variable, the function can directly modify the value of the variable.
#include <stdio.h>
void swap(int* x, int* y) {
int temp = *x;
*x = *y;
*y = temp;
}
int main() {
int a, b;
printf("Please enter the first integer: ");
scanf("%d", &a);
printf("Please enter the second integer: ");
scanf("%d", &b);
printf("Before exchange: a = %d, b = %d ", a, b);
swap(&a, &b);
printf("After exchange: a = %d, b = %d ", a, b);
return 0;
}
6. Method 6: Use arrays This method uses arrays to exchange values of two numbers. Store two numbers in an array and then exchange their values through the array index.
#include <stdio.h>
int main() {
int arr[2];
printf("Please enter the first integer: ");
scanf("%d", &arr[0]);
printf("Please enter the second integer: ");
scanf("%d", &arr[1]);
printf("Before exchange: a = %d, b = %d ", arr[0], arr[1]);
arr[0] = arr[0] ^ arr[1];
arr[1] = arr[0] ^ arr[1];
arr[0] = arr[0] ^ arr[1];
printf("After exchange: a = %d, b = %d ", arr[0], arr[1]);
return 0;
}
7. Method 7: Use structure This method uses structures to exchange values of two numbers. Store two numbers in a structure and then exchange them through structure variables.
#include <stdio.h>
void swap(struct { int x; int y; } *p) {
p->x = p->x ^ p->y;
p->y = p->x ^ p->y;
p->x = p->x ^ p->y;
}
int main() {
struct { int x; int y; } num;
printf("Please enter the first integer: ");
scanf("%d", &num.x);
printf("Please enter the second integer: ");
scanf("%d", &num.y);
printf("Before exchange: a = %d, b = %d ", num.x, num.y);
swap(&num);
printf("After exchange: a = %d, b = %d ", num.x, num.y);
return 0;
}
13. C Language Example - Judge odd numbers Even numbers
1. Method 1: Use if - else statement
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main() {
int number;
// Prompt the user to enter an integer
printf("Please enter an integer: ");
scanf("%d", &number);
// Determine whether the integer is an odd or even number
if (number % 2 == 0) {
printf("%d is an even number. ", number);
}
else {
printf("%d is an odd number. ", number);
}
return 0;
}
2. Method 2: Use the trigonometric operator
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main() {
int number;
// Prompt the user to enter an integer
printf("Please enter an integer: ");
scanf("%d", &number);
// Use the trinocular operator to determine odd or even numbers
(number % 2 == 0) ? printf("%d is an even number. ", number) : printf("%d is an odd number. ", number);
return 0;
}
3. Method 3: Use function encapsulation
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
// Function declaration
void checkEvenOdd(int number);
int main() {
int number;
// Prompt the user to enter an integer
printf("Please enter an integer: ");
scanf("%d", &number);
// Call function to determine odd or even numbers
checkEvenOdd(number);
return 0;
}
// Function definition
void checkEvenOdd(int number) {
if (number % 2 == 0) {
printf("%d is an even number. ", number);
}
else {
printf("%d is an odd number. ", number);
}
}
#include<stdio.h>
void a(int b) {
(b % 2 == 0)? (printf("%d is an even number ", b)) : (printf("%d is an odd number ", b));
}
int main() {
int b;
printf("Please enter a number: ");
scanf("%d", &b);
a(b);
return 0;
}
cycle
Using for loops
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
// Function declaration
void checkEvenOdd(int number);
int main() {
int number;
// Use the for loop to continuously require the user to enter integers until 0 is entered
for (;;) {
// Prompt the user to enter an integer
printf("Please enter an integer (enter 0 to end): ");
scanf("%d", &number);
// If the user enters 0, the loop ends
if (number == 0) {
break;
}
// Call function to determine odd or even numbers
checkEvenOdd(number);
}
printf("Program end. ");
return 0;
}
// Function definition
void checkEvenOdd(int number) {
if (number % 2 == 0) {
printf("%d is an even number. ", number);
}
else {
printf("%d is an odd number. ", number);
}
}
Using while loop
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
// Function declaration
void checkEvenOdd(int number);
int main() {
int number;
// Use a while loop to continuously require the user to enter integers until 0 is entered
while (1) {
// Prompt the user to enter an integer
printf("Please enter an integer (enter 0 to end): ");
scanf("%d", &number);
// If the user enters 0, the loop ends
if (number == 0) {
break;
}
// Call function to determine odd or even numbers
checkEvenOdd(number);
}
printf("Program end. ");
return 0;
}
// Function definition
void checkEvenOdd(int number) {
if (number % 2 == 0) {
printf("%d is an even number. ", number);
}
else {
printf("%d is an odd number. ", number);
}
}
Using do - while loop
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
// Function declaration
void checkEvenOdd(int number);
int main() {
int number;
// Use the do-while loop to continuously require the user to enter integers until 0 is entered
do {
// Prompt the user to enter an integer
printf("Please enter an integer (enter 0 to end): ");
scanf("%d", &number);
// If the user enters 0, the loop ends
if (number == 0) {
break;
}
// Call function to determine odd or even numbers
checkEvenOdd(number);
} while (1);
printf("Program end. ");
return 0;
}
// Function definition
void checkEvenOdd(int number) {
if (number % 2 == 0) {
printf("%d is an even number. ", number);
}
else {
printf("%d is an odd number. ", number);
}
}
14. C Language Example - Odd and Even Numbers in the Loop Interval Range
Method 1: Use a loop to determine odd and even numbers respectively
#include <stdio.h>
int main() {
int start, end, i;
// Prompt the user to enter the interval range
printf("Please enter the starting value of the interval: ");
scanf("%d", &start);
printf("Please enter the end value of the interval: ");
scanf("%d", &end);
printf("even: ");
printf("odd: ");
for (i = start; i <= end; i ) {
if (i % 2 == 0) {
printf("%d ", i);
}
else {
printf("%d ", i);
}
}
printf(" ");
return 0;
}
#include <stdio.h>
int main() {
int start, end, i, c = 0;
// Prompt the user to enter the interval range
printf("Please enter the starting value of the interval: ");
scanf("%d", &start);
printf("Please enter the end value of the interval: ");
scanf("%d", &end);
printf("even: ");
printf("odd: ");
printf(" ");
for (i = start; i <= end; i , c ) {
if (c % 2 == 0) {
printf(" ");
}
if (i % 2 == 0) {
printf("even: ");
printf("%d ", i);
}
else {
printf("odd: ");
printf("%d ", i);
}
}
printf(" ");
return 0;
}
Method 2: Use two loops to output even and odd numbers respectively
#include <stdio.h>
int main() {
int start, end, i;
// Prompt the user to enter the interval range
printf("Please enter the starting value of the interval: ");
scanf("%d", &start);
printf("Please enter the end value of the interval: ");
scanf("%d", &end);
printf("even: ");
for (i = start; i <= end; i ) {
if (i % 2 == 0) {
printf("%d ", i);
}
}
printf(" Odd number: ");
for (i = start; i <= end; i ) {
if (i % 2 != 0) {
printf("%d ", i);
}
}
printf(" ");
return 0;
}
Method 3: Use a loop to store even and odd numbers into the array respectively, and finally output
#include <stdio.h>
int main() {
int start, end, i;
int even[100], odd[100];
int even_count = 0, odd_count = 0;
// Prompt the user to enter the interval range
printf("Please enter the starting value of the interval: ");
scanf("%d", &start);
printf("Please enter the end value of the interval: ");
scanf("%d", &end);
// traverse all integers in the interval range
for (i = start; i <= end; i ) {
if (i % 2 == 0) {
even[even_count ] = i;
}
else {
odd[odd_count ] = i;
}
}
// Output even numbers
printf("even: ");
for (i = 0; i < even_count; i ) {
printf("%d", even[i]);
}
printf(" ");
// Output odd numbers
printf("odd: ");
for (i = 0; i < odd_count; i ) {
printf("%d ", odd[i]);
}
printf(" ");
return 0;
}
I
#include<stdio.h>
int main() {
int start, end, i, a[100], b[100], a_c = 0, b_c = 0;
printf("Please enter the starting value of the interval:");
scanf("%d", &start);
printf("Please enter the end value of the interval:");
scanf("%d", &end);
for (i = start; i <= end; i ) {
if (i % 2 == 0) {
a[a_c ] = i;
}
else {
b[b_c ] = i;
}
}
printf("even number:");
for (i = 0; i < a_c; i ) {
printf("%d ", a[i]);
}
printf(" ");
printf("odd number:");
for (i = 0; i < b_c; i ) {
printf("%d ", b[i]);
}
printf(" ");
return 0;
}
15. C Language Example - Judge Vowel Consonant
Method 1: Use conditional statements This is the most direct method, and determine whether a character is a vowel through conditional statements.
#include <stdio.h>
int main() {
char ch;
// Prompt the user to enter a character
printf("Please enter a character:");
scanf("%c", &ch);
// Convert characters to lowercase for unified judgment
if (ch >= 'A' && ch <= 'Z') {
ch = ch 32;
}
// Determine whether the character is a vowel
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
printf("The input character '%c' is a vowel. ", ch);
}
// If it is not a vowel, determine whether it is a consonant
else if ((ch >= 'a' && ch <= 'z')) {
printf("The input character '%c' is the consonant. ", ch);
}
// If neither vowel nor consonant (such as numbers or special characters), the error message is output
else {
printf("The input character '%c' is neither a vowel nor a consonant. ", ch);
}
return 0;
}
Method 2: Use arrays and loops. This method stores vowels in an array, and loops to determine whether characters are in the array.
#include <stdio.h>
int main() {
char ch;
char vowels[] = { 'a', 'e', 'i', 'o', 'u' };
int isVowel = 0;
// Prompt the user to enter a character
printf("Please enter a character:");
scanf("%c", &ch);
// Convert characters to lowercase for unified judgment
if (ch >= 'A' && ch <= 'Z') {
ch = ch 32;
}
// Determine whether the character is a vowel
for (int i = 0; i < 5; i ) {
if (ch == vowels[i]) {
isVowel = 1;
//Any, number
break;
}
}
if (isVowel) {
printf("The input character '%c' is a vowel. ", ch);
}
// If it is not a vowel, determine whether it is a consonant
else if ((ch >= 'a' && ch <= 'z')) {
printf("The input character '%c' is the consonant. ", ch);
}
// If neither vowel nor consonant (such as numbers or special characters), the error message is output
else {
printf("The input character '%c' is neither a vowel nor a consonant. ", ch);
}
return 0;
}
I'll change it out
#include <stdio.h>
void name(char ch) {
char vowels[] = { 'a', 'e', 'i', 'o', 'u' };
int isVowel = 0;
// Convert characters to lowercase for unified judgment
if (ch >= 'A' && ch <= 'Z') {
ch = ch 32;
}
// Determine whether the character is a vowel
for (int i = 0; i < 5; i ) {
if (ch == vowels[i]) {
isVowel = 1;
break;
}
}
if (isVowel) {
printf("The input character '%c' is a vowel. ", ch);
}
// If it is not a vowel, determine whether it is a consonant
else if ((ch >= 'a' && ch <= 'z')) {
printf("The input character '%c' is the consonant. ", ch);
}
// If neither vowel nor consonant (such as numbers or special characters), the error message is output
else {
printf("The input character '%c' is neither a vowel nor a consonant. ", ch);
}
}
int main() {
char ch;
// Prompt the user to enter a character
printf("Please enter a character:");
scanf("%c", &ch);
// Call the name function to determine the input characters
name(ch);
return 0;
}
cycle
#include <stdio.h>
void name(char ch) {
char vowels[] = { 'a', 'e', 'i', 'o', 'u' };
int isVowel = 0;
// Convert characters to lowercase for unified judgment
if (ch >= 'A' && ch <= 'Z') {
ch = ch 32;
}
// Determine whether the character is a vowel
for (int i = 0; i < 5; i ) {
if (ch == vowels[i]) {
isVowel = 1;
break;
}
}
if (isVowel) {
printf("The input character '%c' is a vowel. ", ch);
}
// If it is not a vowel, determine whether it is a consonant
else if ((ch >= 'a' && ch <= 'z')) {
printf("The input character '%c' is the consonant. ", ch);
}
// If neither vowel nor consonant (such as numbers or special characters), the error message is output
else {
printf("The input character '%c' is neither a vowel nor a consonant. ", ch);
}
}
int main() {
char ch;
while (1) {
// Prompt the user to enter a character
printf("Please enter a character (enter '0' to end):");
scanf(" %c", &ch); // Note the space here, which is used to ignore the newlines left by the previous input
// Check whether to enter an end character (for example '0')
if (ch == '0') {
break;
}
// Call the name function and pass the characters entered by the user as parameters
name(ch);
}
return 0;
}
Method 3: Use string search This method stores vowels in a string, and uses the strchr function to find whether the characters are in the string.
#include <stdio.h>
#include <string.h>
int main() {
char ch;
char vowels[] = "aeiou";
char* result;
// Prompt the user to enter a character
printf("Please enter a character:");
scanf("%c", &ch);
// Convert characters to lowercase for unified judgment
if (ch >= 'A' && ch <= 'Z') {
ch = ch 32;
}
// Determine whether the character is a vowel
result = strchr(vowels, ch);
if (result != NULL) {
printf("The input character '%c' is a vowel. ", ch);
}
// If it is not a vowel, determine whether it is a consonant
else if ((ch >= 'a' && ch <= 'z')) {
printf("The input character '%c' is the consonant. ", ch);
}
// If neither vowel nor consonant (such as numbers or special characters), the error message is output
else {
printf("The input character '%c' is neither a vowel nor a consonant. ", ch);
}
return 0;
}
cycle
Method 1: Use while (1) loop and conditional statements This is the most direct method to control exit through infinite loops and conditional statements.
#include <stdio.h>
int main() {
char ch;
while (1) {
// Prompt the user to enter a character
printf("Please enter a character (enter 'q' to exit):");
scanf(" %c", &ch); // Note the spaces here, which are used to clear newlines in the buffer
// Check whether the loop is exited
if (ch == 'q') {
printf("Exit program. ");
break;
}
// Convert characters to lowercase for unified judgment
if (ch >= 'A' && ch <= 'Z') {
ch = ch 32;
}
// Determine whether the character is a vowel
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
printf("The input character '%c' is a vowel. ", ch);
}
// If it is not a vowel, determine whether it is a consonant
else if ((ch >= 'a' && ch <= 'z')) {
printf("The input character '%c' is the consonant. ", ch);
}
// If neither vowel nor consonant (such as numbers or special characters), the error message is output
else {
printf("The input character '%c' is neither a vowel nor a consonant. ", ch);
}
}
return 0;
}
My improvement
1. Release it
#include <stdio.h>
void name(char ch) {
char vowels[] = { 'a', 'e', 'i', 'o', 'u' };
int isVowel = 0;
// Prompt the user to enter a character
/*printf("Please enter a character:");
scanf("%c", &ch);*/
// Convert characters to lowercase for unified judgment
if (ch >= 'A' && ch <= 'Z') {
ch = ch 32;
}
// Determine whether the character is a vowel
for (int i = 0; i < 5; i ) {
if (ch == vowels[i]) {
isVowel = 1;
break;
}
}
if (isVowel) {
printf("The input character '%c' is a vowel. ", ch);
}
// If it is not a vowel, determine whether it is a consonant
else if ((ch >= 'a' && ch <= 'z')) {
printf("The input character '%c' is the consonant. ", ch);
}
// If neither vowel nor consonant (such as numbers or special characters), the error message is output
else {
printf("The input character '%c' is neither a vowel nor a consonant. ", ch);
}
}
int main() {
char ch;
printf("Please enter a character:");
scanf("%c", &ch);
name(ch);
return 0;
}
2. cycle
#include <stdio.h>
void name(char ch) {
char vowels[] = { 'a', 'e', 'i', 'o', 'u' };
int isVowel = 0;
// Convert characters to lowercase for unified judgment
if (ch >= 'A' && ch <= 'Z') {
ch = ch 32;
}
// Determine whether the character is a vowel
for (int i = 0; i < 5; i ) {
if (ch == vowels[i]) {
isVowel = 1;
break;
}
}
if (isVowel) {
printf("The input character '%c' is a vowel. ", ch);
}
// If it is not a vowel, determine whether it is a consonant
else if ((ch >= 'a' && ch <= 'z')) {
printf("The input character '%c' is the consonant. ", ch);
}
// If neither vowel nor consonant (such as numbers or special characters), the error message is output
else {
printf("The input character '%c' is neither a vowel nor a consonant. ", ch);
}
}
int main() {
char ch;
while (1) {
// Prompt the user to enter a character
printf("Please enter a character:");
scanf(" %c", &ch); // Note the space here, which is used to ignore the newlines left by the previous input
// Check whether to enter an end character (for example '0')
if (ch == '0') {
break;
}
// Call the name function and pass the characters entered by the user as parameters
name(ch);
}
return 0;
}
Method 2: Use do - while loop The do - while loop will be executed at least once, and then decide whether to continue execution based on the conditions.
#include <stdio.h>
#include <string.h>
int main() {
char ch;
char vowels[] = "aeiou";
char* result;
do {
// Prompt the user to enter a character
printf("Please enter a character (enter 'q' to exit):");
scanf(" %c", &ch); // Note the spaces here, which are used to clear newlines in the buffer
// Convert characters to lowercase for unified judgment
if (ch >= 'A' && ch <= 'Z') {
ch = ch 32;
}
// Determine whether the character is a vowel
result = strchr(vowels, ch);
if (result != NULL) {
printf("The input character '%c' is a vowel. ", ch);
}
// If it is not a vowel, determine whether it is a consonant
else if ((ch >= 'a' && ch <= 'z')) {
printf("The input character '%c' is the consonant. ", ch);
}
// If neither vowel nor consonant (such as numbers or special characters), the error message is output
else {
printf("The input character '%c' is neither a vowel nor a consonant. ", ch);
}
} while (ch != 'q');
printf("Exit program. ");
return 0;
}
Method 3: Use for loops Although for loops are usually used for loops with known times, for(;;) can also be used to implement infinite loops.
#include <stdio.h>
#include <string.h>
int main() {
char ch;
char vowels[] = "aeiou";
char* result;
for (;;) {
// Prompt the user to enter a character
printf("Please enter a character (enter 'q' to exit):");
scanf(" %c", &ch); // Note the spaces here, which are used to clear newlines in the buffer
// Check whether the loop is exited
if (ch == 'q') {
printf("Exit program. ");
break;
}
// Convert characters to lowercase for unified judgment
if (ch >= 'A' && ch <= 'Z') {
ch = ch 32;
}
// Determine whether the character is a vowel
result = strchr(vowels, ch);
if (result != NULL) {
printf("The input character '%c' is a vowel. ", ch);
}
// If it is not a vowel, determine whether it is a consonant
else if ((ch >= 'a' && ch <= 'z')) {
printf("The input character '%c' is the consonant. ", ch);
}
// If neither vowel nor consonant (such as numbers or special characters), the error message is output
else {
printf("The input character '%c' is neither a vowel nor a consonant. ", ch);
}
}
return 0;
}
Method 4: Use flag variables Use a flag variable to control the continuation or exit of the loop.
#include <stdio.h>
#include <string.h>
int main() {
char ch;
char vowels[] = "aeiou";
char* result;
int continueLoop = 1;
while (continueLoop) {
// Prompt the user to enter a character
printf("Please enter a character (enter 'q' to exit):");
scanf(" %c", &ch); // Note the spaces here, which are used to clear newlines in the buffer
// Check whether the loop is exited
if (ch == 'q') {
printf("Exit program. ");
continueLoop = 0;
}
// Convert characters to lowercase for unified judgment
if (ch >= 'A' && ch <= 'Z') {
ch = ch 32;
}
// Determine whether the character is a vowel
result = strchr(vowels, ch);
if (result != NULL) {
printf("The input character '%c' is a vowel. ", ch);
}
// If it is not a vowel, determine whether it is a consonant
else if ((ch >= 'a' && ch <= 'z')) {
printf("The input character '%c' is the consonant. ", ch);
}
// If neither vowel nor consonant (such as numbers or special characters), the error message is output
else {
printf("The input character '%c' is neither a vowel nor a consonant. ", ch);
}
}
return 0;
}