1. 程式人生 > >《明解C語言》筆記及課後習題答案 【第九章】

《明解C語言》筆記及課後習題答案 【第九章】

練習9-1

/*---輸出字元陣列char str[] = "ABC\0DEF"---*/ 

#include <stdio.h>

int main(void)
{
	char str[] = "ABC\0DEF";
	
	printf("字串str為\"%s\"。\n", str);
	
	return 0;
}

練習9-2

/*---讓該初始化賦值得到的字串s變成空字串:char s[] = "ABC"---*/

#include <stdio.h>

int main (void)
{
	char s[] = "ABC";
	
	s[0] = '\0';
	
	printf("字串s為:%s",s);
	
	return 0;
}

練習9-3

/*對程式碼清單9-7進行改寫*/

#include <stdio.h>

#define NUMBER 5

int main(void)
{
	int i;
	char s[NUMBER][128];
	
	for (i = 0; i < NUMBER; i++) {
		printf("s[%d]:", i);
		scanf("%s", s[i]);
		if (strcmp(s[i], "$$$$$") == 0) 
			break;
	}
	
	for(i = 0; i < NUMBER; i++){
		if (strcmp(s[i], "$$$$$") == 0)
			break;
		printf("s[%d] = \"%s\"\n", i, s[i]);
	}
	
	return 0;
 } 

練習9-4

/*---編寫一個函式,使字串s為空字串。---*/

#include <stdio.h>

void null_string(char s[]){
	s[0] = '\0';
	
	printf("字串s為:%s",s);
}

int main(void)
{
	char s[] = "HELLOWORLD";
	
	null_string(s);
	
	return 0;
 } 

練習9-5

/*---編寫函式,若字串s中含有字元c(若含有多個,以先出現的為準),則返回該元素的下標。---*/

#include <stdio.h>

int str_char(const char s[], int c) {
	int idx = 0;
	
	while (s[idx]){
		if(s[idx] == 'c')
			return idx;
		idx++;
	}
	
	return -1;
}

int main(void)
{
	char c;
	char s[] = "hellochina";
	
	printf("字元c在字串%s中的下標為:%d", s, str_char(s,c));
	
	return 0;
 } 

練習9-6

/*---編寫函式,返回字串s中字元c的個數---*/

#include <stdio.h>

int str_chnum(const char s[], int c){
	int num = 0, idx = 0;
	
	while (s[idx]){
		if(s[idx] == 'c')
			num++;
		idx++;
	}
	return num;
	
	if(num == 0)
		return 0;
}

int main(void)
{
	char c = 'c';
	
	char s[] = "hellochinanancang";
	
	printf("字串%s中%c字元的個數為%d", s, c, str_chnum(s, c));
	
	return 0;
 } 

練習9-7

/*---編寫函式,使字串s顯示n次。---*/

#include <stdio.h>

void put_stringn(const char s[], int n){
	int num, i = 0;
	
	for (num = 0; num < n; num++){
		//printf("%s\n",s);
		while(s[i]){
			putchar(s[i++]);
		}
		i = 0;
	}
		
	return 0;
}

int main(void)
{
	char s[] = "多重影分身之術!";
	
	int n = 100;
	
	put_stringn(s, n);
	
	return 0;
}

練習9-8

/*---編寫函式,實現字串的逆向輸出。---*/

#include <stdio.h>

void put_stringr(const char s[]){
	int num = 0, i = 0;
	while (s [num])
		num++;
	while (i < num){
		putchar(s[num - i -1]);
		i++;
	}
}

int main(void)
{
	char s [] = "hello";
	
	put_stringr(s);
	
	return 0;
}

練習9-9

/*---逆向顯示字串s的字元---*/

#include <stdio.h>

void rev_string(char s[]){
	int num = 0, i = 0, temp;
	while (s [num])
		num++;
	for(i = 0; i < num/2; i++){
		temp = s[i];
		s[i] = s[num - i -1];
		s[num - i -1] = temp;
	}	
	printf("%s",s);
}

int main(void)
{
	char s[] = "hello";
	
	rev_string(s);
	
	return 0;
 } 

練習9-10

/*---將字串s中的數字字元全部刪除。---*/

#include <stdio.h>

void del_digit(char s[]) {
    int num, i = 0, temp ;
    
    while (s[i]) {
        if (s[i] >= '0' && s[i] <= '9'){
            temp = i;
            while(s[temp]) {            //將0~9數字通過遍歷使其被最後一位元素‘\0’替換。
                s[temp] = s[temp + 1];
                temp++;
            }
            i--;
        }
        i++;
    }
    printf("%s",s);
}

int main (void)
{
    char s[] = "adb34fd43";
    
    del_digit(s);
    
    return 0;
}

練習9-11

/*---對程式碼清單進行改寫---*/

#include <stdio.h>

#define NUMBER 5
#define IDX_NUM 128
void put_strary(const char s[][IDX_NUM], int n)
{
	int i;
	for (i = 0; i < n; i++){
		if (strcmp(s[i], "$$$$$") == 0) 
			break;
		printf("s[%d] = \"%s\"\n", i, s[i]);
	}
 } 
 
 int main(void)
 {
 	int i;
 	
 	char cs[NUMBER][IDX_NUM];
 	
 	puts("請輸入五個字串。");
 	for(i = 0; i < NUMBER; i++){
 		scanf("%s",cs[i]);
 		if (strcmp(cs[i], "$$$$$") == 0) 
			break;
	 } 
 	
 	put_strary(cs, NUMBER);
 	
 	return 0;
 }

練習9-12

/*---將所接收的字串陣列中儲存的n個字串的字元逆向顯示。---*/

#include <stdio.h>

void rev_string(char s[][128], int n) {
	int i=0, j=0, a = 0, temp;
	
	for (i = 0; i < n; i++){		//遍歷字串陣列 
	while(s[i][j]) {
		j++; 						//計算不同字串的長度 
	}
	for(a = 0; a < j/2; a++){		//對字串進行逆向轉換 
		temp = s[i][a];
		s[i][a] = s[i][j - a - 1];
		s[i][j - a -1] = temp;
	}
}

}

int main (void)
{
	int n = 3, i;
	char s[][128] = {"sec", "abc","cartman"};
	
	rev_string(s,n);
	
	for(i = 0;i < n; i++)
		printf("%s\n",s[i]);
		
	return 0;
}

字串字面量:像"ABC"那樣帶雙引號的一系列字元稱為字串字面量(string literal)。

由3個字元組成的字串字面量“ABC”實際上佔用了4個字元的記憶體空間。在字串字面量的末尾會被加上一個

叫作null字元的值為0的字元。用八進位制轉義字元表示null字元就是‘\0’。整數常量就是0。

字串:字串最適合放在char陣列中儲存。

字串的讀取:為了從標準輸入讀取字串,需要把scanf函式的轉換說明設為%s,還必須傳入陣列名。請注意這裡的name是不帶&運算子的。另外,scanf函式在將從鍵盤讀取的字串儲存到陣列中時,會在末尾加上null字元。

格式化顯示字串:

                                             

strcmp函式:對兩個字串進行相等比較,若兩個字串相等,則返回0。