Download presentation
Presentation is loading. Please wait.
1
String C語言-字串
2
字串(String) 字串是一個結構性的資料型態,它的實施方式有兩種,一種是使用字元陣列,另一種是使用指標.
字串的結構與一維的字元陣列是一樣的,兩者的差別在於字串是以空字元'\0'結束. 字串以空字元'\0'來當作字串的結束值,它是函數處理字串所認定字串結尾的唯一方法.
3
字串的宣告 字元陣列方式 指標方式 char 字串變數名稱[字元個數]; char 字串變數名稱[字元個數]="字串常數";
char s2[13]; /* 宣告字串變數 s2 ,最多13個字元(包含空字元)*/ char 字串變數名稱[字元個數]="字串常數"; char s2[13]=“I like C.”; /*宣告字串變數 s2 ,不指定長度 ,初值為字串常數“I like C.” */ 指標方式 char *字串指標變數名稱; char *s2; /* 宣告字串指標變數 s2*/ char *字串指標變數名稱="字串常數"; char *s2=“I like C.”; /*宣告字串指標變數 s2,初值為字串常數“I like C.” */ I l i k e C . \0 I l i k e C . \0
4
範例1-印出字元及字串的長度 #include <stdio.h> int main(void) { char a[]="My friend"; char b='c'; char str[]="c"; printf("sizeof(a)=%d\n",sizeof(a)); printf("sizeof(b)=%d\n",sizeof(b)); printf("sizeof(str)=%d\n",sizeof(str)); system("pause"); return 0; }
5
範例2-以字元陣列及字元指標變數表示字串 #include<stdio.h> int main( ) { int i; char s1[13]; char s2[13]="I like C."; char *s3; char *s4="Thank you!"; printf("s2=%s\n",s2); printf("s4=%s\n",s4); /*印出字串指標變數s4所指的字串*/ for(i=0;i<13;i++) s1[i]=s2[i]; /*將s2 copy至s1*/ printf("s2=s1,then s1=%s\n",s1); s3=s4; printf("s3=s4,then s3=%s\n",s3); /*印出印出字串指標變數s3所指的字串*/ s3=s2; printf("s3=s2,then s3=%s\n",s3); printf("The 3nd character of s4 is '%c'. \n ",*(s4+2)); system("pause"); return 0; }
6
範例3-字串資料的輸入與輸出 #include<stdio.h> int main( ) { char *s1="I love C"; char temp[20]; char *s2=&temp[0]; char s3[20]; char s4[]="I like C"; printf("The s1 are:%s\n",s1); printf("The s2 are:"); gets(s2); puts(s2); printf("Enter s3 string: "); scanf("%s",&s3); printf("The s3 are:%s\n",s3); printf("The s4 are:%s\n",s4); system("pause"); return 0; }
7
範例4-字串參數以call by address傳遞
#include<stdio.h> void change(char *); int main( ) { char s[ ]="abcdefg"; char temp[ ]="ijklmnop"; char *t=&temp[0]; change(&s[0]); change(t); puts(s); puts(t); system("pause"); return 0; } void change(char *x) *(x+2)='$'; *(x+5)='+'; s a b c d e f g \0 $ + t i j k l m n o p \0 $ +
8
字串常用的String Library Function
C語言提供很多字串庫存函數,這些都存在string.h標頭檔裡 名稱 寫法 用途 strcat ( ) strcat(str1,str2) 將str2串接在str1之後 strncat ( ) strncat(str1,str2,n) 將str2的前面n個字元,串接在str1之後 strcpy ( ) strcpy(str1,str2) 將一個str2 複製到另一str1中 strncpy strncpy(str1,str2,n) 將str2前面 n個字元複製至str1字串 strcmp( ) strcmp(str1,str2) 將str1和str2由左至右依序比較其字元(根據ASCII值), 傳回一個函數值: 函數值<0str1<str2 函數值=0str1=str2 函數值>0str1>str2 strlen( ) strlen(str1) 傳回str1的長度(含空白字元,但不含結束字元\0) strchr( ) strchr(str1,ch) 根據字元的ASCII值,與str1比對,若無相同的ASCII值,則傳回空指標;若比對相同,則將初次出現的位址傳回 strstr( ) strstr(str1,str2) 從str2開始與str1比對,若無一樣的子字串,則傳回空指標;若一樣,則傳回str1比對一樣的位址 補充:strrev() 倒印
9
範例5-strcat() #include<stdio.h> #include<string.h> int main( ) { int i; char s1[40]="abcde"; char s2[40]="fghijk"; strcat(s1,s2); printf("The new string are=%s\n",s1); system("pause"); return 0; } s1 a b c d e \0 s2 f g h i j k \0 s1 a b c d e f g h i j k \0
10
範例6-strncat() #include<stdio.h> #include<string.h> int main( ) { int i; char s1[40]="abcde"; char s2[40]="fghijk"; strncat(s1,s2,3); printf("The new string are=%s\n",s1); system("pause"); return 0; } s1 a b c d e \0 s2 f g h i j k \0 s1 a b c d e f g h \0
11
範例7-strcpy() #include<stdio.h> #include<string.h> int main( ) { int i; char s1[40]="abcde"; char s2[40]="fghijk"; strcpy(s1,s2); printf("The new string are=%s\n",s1); system("pause"); return 0; } s1 a b c d e \0 s2 f g h i j k \0 s1 f g h i j k \0
12
範例8-strncpy() #include<stdio.h> #include<string.h> int main( ) { int i; char s1[40]="abcde"; char s2[40]="fghijk"; strncpy(s1,s2,3); printf("The new string are=%s\n",s1); system("pause"); return 0; } s1 a b c d e \0 s2 f g h i j k \0 s1 f g h d e \0
13
範例9-strcmp() #include<stdio.h> #include<string.h> int main( ) { int i,p; char s1[40]="abcde"; char s2[40]="fghijk"; p=strcmp(s1,s2); printf("strcmp(s1,s2)=%d\n",p); system("pause"); return 0; } s1 a b c d e \0 s2 f g h i j k \0 a(97) – f(102) = -1
14
範例10-strlen() #include<stdio.h> int main( ) { int i,p,q;
#include<string.h> int main( ) { int i,p,q; char s1[40]="abcde"; char s2[40]="fghijk"; p=strlen(s1); q=strlen(s2); printf("s1=%d;s2=%d\n",p,q); system("pause"); return 0; }
15
範例11-strchr(),strstr() #include<stdio.h> #include<string.h> int main( ) { char *p,*q,*ch2="am"; int ch1='a'; char *s1="I am OK"; char *s2="I am OK"; p=strchr(s1,ch1); q=strstr(s2,ch2); printf("s1 serach 'a'=%s ; s2 search 'am'=%s\n",p,q); system("pause"); return 0; }
Similar presentations