1. 程式人生 > >字符串字符數組

字符串字符數組

fin strcmp oid ets 清理內存 n) include blog 類型

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include <stdio.h>
 3 #include <stdlib.h>
 4 
 5 int main()
 6 {
 7 
 8     /*
 9     字符串的賦值:
10     給 char* 類型的字符串賦值,可以直接使用 "=" 號
11     給 char[] 類型的字符串賦值,需要使用 strcpy 函數
12 
13   字符串的特點:
14     需要明白的一點就是字符串以\0結尾, 沒有\0就不是字符串
15     只要是用雙引號括起來的都是字符串
16     字符串的本質就是數組(字符數組)
17 */ 18 19 /* 20 輸入: 21 利用scanf函數接收字符串 22 利用gets函數接收字符串 23 利用fgets函數接收字符串(推薦常用!) 24 */ 25 printf("請輸入一個字符串:"); 26 char name[12]; 27 //scanf("%s", name);//利用scanf函數接收字符串,會以空格、Tab、回車 作為結束符 28 29 //gets(name);//利用gets函數接收字符串,可以輸入空格、Tab,以回車作為結束符; 30 //
不安全,輸入字符串沒有長度限制,無限輸入會造成內存溢出 31 32 fgets(name, 12, stdin); 33 //解決了安全性問題 34 //fgets(參數1,參數2, 參數3) 35 //參數1:保存數據的首位置 36 //參數2:保存的長度(包括 ‘\0‘) 37 //參數3:stdin(標準控制臺輸入) 38 //接收空格、Tab,以回車作為結束符 39 40 printf("name = %s\n", name); 41 42 /* 43 輸出: 44   %s的原理, 從傳入的"地址"開始逐個取出, 直到遇到"\0"位置   
45 46   如何輸出字符串: 47 使用printf的%s占位符來輸出 48 使用puts函數來輸出(自動換行,原樣輸出) 49 */ 50 51 char str[] = "how are you"; 52 printf("%s\n", str); // 使用printf的%s占位符來輸出 53 54 puts(str);//使用puts函數來輸出(自動換行,原樣輸出) 55 56 system("pause"); 57 return 0; 58 }
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 // 數組賦值三種方式
 4 
 5 void mystrcpy(char *str1, const char *str2)
 6 {
 7     // *str2對*str1逐個字符進行賦值
 8     // *str2直到把‘\0‘賦值給*str1時,*str1的結果就是0,循環就結束!
 9     while ((*str1++ == *str2++));
10     
11 }
12 
13 int main()
14 {
15     char str[10] = "abc";
16 
17     // 使用循環給字符數組賦值
18     for (int i = 0; i < 10; i++)
19     {
20         str[i] = "ABC"[i];  // 等價於 *("ABC"+i),"ABC"返回的是A的地址(即首地址)==> 替換
21         printf("%s\n", str);  //str = ABC
22     }
23 
24     // 使用標準庫函數給字符數組賦值
25     strcpy(str, "XYZ");
26     printf("str = %s\n", str);
27 
28     // 使用自定義函數給字符數組賦值
29     mystrcpy(str, "OKOK");
30     printf("str = %s\n", str);
31 
32 
33     system("pause");
34     return 0;
35 }
 1 // 庫函數
 2 #include <stdio.h>
 3 #include <stdlib.h>
 4 #include <string.h>
 5 
 6 int main()
 7 {
 8     // 計算字符串的長度(strlen):(計算字符串中有多少個字符,註意不包括\0)
 9     // strlen的原理 : 從傳入的地址開始逐個取出字符串, 每取出一個就讓計數器 + 1.直到遇到\0為止。
10 
11     char str[] = "how are you"; // 定義字符數組
12 
13     int len = sizeof(str) / sizeof(str[0]) - 1;
14     printf("%d\n", len);
15     //printf("%d\n", strlen(str)); // 計算結果不包括 \0
16 
17     //字符串拼接(strcat)
18     //原理 : 首先遍歷第一個字符串, 直到遇到\0為止, 
19     //然後取出第二個字符串中的字符, 從\0的位置開始添加, 添加完畢之後再在最後添加一個\0
20     void Strcat(char *s1, char *s2)
21     {
22         // 找位置
23         while (*s1) s1++;
24 
25         // 拷貝
26         while(*s1++ = *s2++)
27     }
28 
29     // 字符串拷貝(strcpy)
30     // strcpy函數會將源的數據拷貝到目標中, 
31     // 並且會覆蓋掉目標中原有的數據,目標的容積必須能夠存放拷貝的數據, 如果容積不夠會報錯。
32 
33     void Strcpy(char *s1, char *s2)
34     {
35         while (1)
36         {
37             *s1 = *s2;
38             if (*s1 == \0) break;
39             s1++;
40             s2++;
41         }
42 
43         //while ((*s1++ = *s2++));
44         
45     }
46 
47     // 字符串比較(strcmp)
48     // 原理 : 取出字符串中的每一個字符進行逐個比較, 如果發現不相等就不會繼續往下比較
49 
50     /* strcpy 字符串賦值函數 */
51     void test1() {
52         char str[6] = { 0 };//表示在棧中分配了6個字節的內存空間,空間的首地址是str(數組名)
53         strcpy(str, "abc");//給字符數組賦值,str[10] = "abc";
54         strncpy(str, "AABBCC", sizeof(str) - 2);//只賦值前4個字符(AABB);str[6] = "AABB";
55     }
56     /* strcat 給一個字符串追加內容 */
57     void test2() {
58         char str[8] = "abc";
59         strcat(str, "def"); //str[8] = "abcdef";
60         strncat(str, "kkkkkk", 3);//只追加前3個字符; str[8] = "abckkk";
61     }
62     /* strcmp 比較字符串內容的大小 */
63     void test3() {
64         char *str1 = "abcd.c";
65         char *str2 = "abcf.m";
66         strcmp(str1, str2); //返回值為: -2 (表示 str1 < str2)
67         strncmp(str1, str2, 3); //比較前3個字符的大小; 返回值為: 0 (表示 str1 == str2)
68         bool r = str1 > str2;//比較地址大小(str1和str2都是地址)
69     }
70     /* memset 內存清理函數(清空) */
71     void test4() {
72         char str[8] = "abcd";
73         memset(str, 0, sizeof(str));//清理內存空間(開始位置, 清零, 空間大小/長度);
74         strcpy(str, "ABCD");//清空後重新賦值
75         printf("str = %s\n", str);
76     }
77     system("pause");
78     return 0;
79 }

字符串字符數組