8.5 (字元檢查)請撰寫一個程式由鍵盤輸入一個字元,然後用字元處理函式庫的每一個函式來檢查這個字元。你的程式應 印出每一個函式所傳回來的值。

8.5 (Character Testing) Write a program that inputs a character from the keyboard and tests it with each of the functions in the character-handling library. The program should print the value returned by each function.

#include <stdio.h>
#include <ctype.h>

int main() {
    char c;

    printf("character: ");
    scanf("%c", &c);

    printf("isalnum(c): %d\\n", isalnum(c));
    printf("isalpha(c): %d\\n", isalpha(c));
    printf("iscntrl(c): %d\\n", iscntrl(c));
    printf("isdigit(c): %d\\n", isdigit(c));
    printf("isgraph(c): %d\\n", isgraph(c));
    printf("islower(c): %d\\n", islower(c));
    printf("isprint(c): %d\\n", isprint(c));
    printf("ispunct(c): %d\\n", ispunct(c));
    printf("isspace(c): %d\\n", isspace(c));
    printf("isupper(c): %d\\n", isupper(c));
    printf("isxdigit(c): %d\\n", isxdigit(c));
    printf("tolower(c): %c\\n", tolower(c));
    printf("toupper(c): %c\\n", toupper(c));

    return 0;
}

方法:

我找到ctype中的所有函數然後全部運行一次

Method:

i find all the function in ctype then run it all once


8.6 以大寫和小寫字母字串互相轉換

Description

請撰寫一個程式,將一行文字輸入到字元陣列s[100]。請將這行文字的大寫字母以小寫字母的形式輸出,小寫字母以大寫字母的形式輸出。

Write a program that inputs a line of text into char array s[100]. Output the line in alternate uppercase letters and lowercase letters.

Input

輸入包含一個字串 s。

Input contains a string s.

Output

請將這行文字的大寫字母以小寫字母的形式輸出,小寫字母以大寫字母的形式輸出。

Output the line in alternate uppercase letters and lowercase letters.

Sample Input 1

Sample Output 1

AGQowysNlpy

agqOWYSnLPY
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<ctype.h>

int main(){
	char s[100];
	
	scanf("%s",s);
	
	for(int i=0; i<=strlen(s); i++){
		if(isupper(s[i])){
			s[i] = tolower(s[i]);
		}else{
			s[i] = toupper(s[i]);
		}
	}
	
	printf("%s",s);
	
	return 0;
} 

方法:

我主要使用 ctype 的擴充。我使用 isisupper 來確定它是否為大寫。那麼如果是,我使用 tolower 將其變為小寫。否則我使用 toupper 使其成為大寫字母。

Method:

I mainly uses extension from ctype. I uses is isupper to determine is it an upper case. then if yes i uses tolower to make it a lower case. else i uses toupper to make it a Capital Letter.


8.7 (將字串轉換成整數並計算)請撰寫一個程式,輸入 6 個代表整數的字串,將這些字串轉換成整數,並計算這 6 個值的 和及平均。

8.7 (Converting Strings to Integers for Calculations) Write a program that inputs six strings that represent integers, converts the strings to integers, and calculates the sum and average of the six values.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

//int strnum(char a[100], int end){
//	if(end==1){
//		return 0;
//	}else{
//		printf("%d",atoi(a));
//		return atoi(a) + strnum(a,end-1);
//	}
//}
int main() {
    char s1[100], s2[100], s3[100], s4[100], s5[100], s6[100];
    scanf("%s", s1);
    scanf("%s", s2);
    scanf("%s", s3);
    scanf("%s", s4);
    scanf("%s", s5);
    scanf("%s", s6);
    int a1 = atoi(s1);
//    ascii to integer in stdlib.h 
//    printf("%d",atoi(s1));

	int a2 = atoi(s2);
	int a3 = atoi(s3);
	int a4 = atoi(s4);
	int a5 = atoi(s5);
	int a6 = atoi(s6);
	
	printf("sum = %d\\n", (a1+a2+a3+a4+a5+a6));
	printf("ave = %d", (a1+a2+a3+a4+a5+a6)/6);

    
    
    
    

    return 0;
}

方法:

我使用 stdlib.h 中的一個函數,它可以將 ascii 程式碼轉換為整數。 atoi(字串)

然後我添加一個新的整數變數來儲存所有轉換後的字串並輸出其總和和平均值

method:

I uses a function that’s in stdlib.h where it can convert ascii code into integer. atoi(string)

then I add a new integer variable to store all the converted string and output its summation and average