標籤:
??字串處理函數#include <string.h> // 字串數組標頭檔 // puts輸出函數 char str[20] = "zyp"; printf("who loves %s\n", str); // 不會自動換行 puts(str); puts("gj"); // 不能進行格式化輸出 // gets輸入函數
char str2[15];
gets(str2); // 使用scanf時不能有空格,gets中間可以有空格
puts(str2); // strcat 字串串連函數
// 返回的是前面的字串的地址,儲存在前面的字串數組中
strcat(str, str2);
puts(str);
printf("串連後的字串為%s\n", str); // strcpy 字串拷貝函數 // strcpy(str1, str2) 把字串2拷貝到字串1中,字串1中的內容被覆蓋 str1的長度 >= str2的長度 strcpy(str, str2);
puts(str); // strcmp 字串比較函數
char str3[] = "ac";
char str4[] = "fun";
printf("%d\n", strcmp(str3, str4)); // strlen 計算字串的長度 char str5[] = "德瑪西亞"; // 一個漢字預設是 utf-8 編碼,佔用3個位元組 printf("%lu\n", strlen(str5)); ??單字首大寫統計單詞個數 // 首先定義一個字串數組
char str[100];
// 定義變數count統計單詞的個數
int count = 0;
int isWord = 1; // 用來判別是不是單詞
// 輸入字串
gets(str);
// 迴圈判斷字元是不是\0
for (int i = 0; str[i] != ‘\0‘; i++) {
// 再判斷是不是單詞
if (str[i] == ‘ ‘) {
isWord = 1; // 等於空格說明這是一個單詞的開始
}else if (isWord == 1){
str[i] = str[i] - 32;
count++;
isWord = 0; // 迴圈得到一個單詞後就把isword歸0
}
} printf("%d\n", count); puts(str);
C語言基礎總結 ( 三 )----------字串總結