Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to the C Programming Language

Similar presentations


Presentation on theme: "Introduction to the C Programming Language"— Presentation transcript:

1 Introduction to the C Programming Language
字串 (String)

2 字串 (String) 字串是一個結構性的資料型態,它的實施方式有兩種,一種是使用陣列結構,另一種是使用指標.
字串以空字元‘\0’來當作字串的結束值,它是函數處理字串所認定字串結尾的唯一方法. 字串的結構與一維的字元陣列是一樣的, 兩者的差別在於字串是以空字元‘\0’結束.

3 字串 (String) 字串的宣告 char 字串變數名稱 [字元個數] ;
例: char s2[13]; /* 宣告字串變數 s2 ,最多13個字元*/ char 字串變數名稱 [字元個數] = “字串常數” ; 例: char s2[ ]=“I like C.”; /*宣告字串變數 s2 ,不指定長度 ,初值為字串常數“I like C.” */ char *字串指標變數名稱 ; 例: char *s2; /* 宣告字串指標變數 s2*/ char *字串指標變數名稱 = “字串常數” ; 例: char *s2=“I like C.”; /*宣告字串指標變數 s2 ,最多13個字元,初值為字串常數“I like C.” */ s2 I l i k e C . \0 I l i k e C . \0

4 字串 (String) 範例一: 以字元陣列及字元指標變數表示字串 #include<stdio.h> 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); }

5 字串 (String) 範例二: 示範字串資料的輸入與輸出 #include<stdio.h> main( ) {
char *s1="I love C"; char *s2="\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); }

6 字串 (String) 範例三: 示範字串參數以call by address傳遞 #include<stdio.h>
main( ) { void change(char *); char s[ ]="abcdefg"; char *t="ijklmnop"; change(s); change(t); puts(s); puts(t) } void change(char *x) *(x+2)='$'; *(x+5)='+';

7 常用的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值), 傳回一個函數值: 函數值<0str1<str2 函數值=0str1=str2 函數值>0str1>str2 strlen( ) strlen(str1) 傳回str1的長度 strchr( ) strchr(str1,ch) 根據ch的ASCII值,與str1比對,若無相同的ASCII值,則傳回空指標;若比對相同,則將出次出現的位址傳回 strstr( ) strstr(str1,str2) 從str2開始與str1比對,若無一樣的子字串,則傳回空指標;若一樣,則傳回str1比對一樣的位址

8 常用的String Library Function
範例四: strcat( ) #include<stdio.h> #include<string.h> main( ) { int i; char s1[40]="abcde"; char s2[40]="fghijk"; strcat(s1,s2,); printf("The new string are=%s\n",s1); }

9 常用的String Library Function
範例五: strncat( ) #include<stdio.h> #include<string.h> main( ) { int i; char s1[40]="abcde"; char s2[40]="fghijk"; strncat(s1,s2,3); printf("The new string are=%s\n",s1); }

10 常用的String Library Function
範例六: strcpy( ) #include<stdio.h> #include<string.h> main( ) { int i; char s1[40]="abcde"; char s2[40]="fghijk"; strcpy(s1,s2); printf("The new string are=%s\n",s1); }

11 常用的String Library Function
範例七: strncpy( ) #include<stdio.h> #include<string.h> main( ) { int i; char s1[40]="abcde"; char s2[40]="fghijk"; strncpy(s1,s2,3); printf("The new string are=%s\n",s1); }

12 常用的String Library Function
範例八: strcmp( ) #include<stdio.h> #include<string.h> main( ) { int i,p; char s1[40]="abcde"; char s2[40]="fghijk"; p=strcmp(s1,s2); printf("strcmp(s1,s2)=%d\n",p); }

13 常用的String Library Function
範例九: strlen( ) #include<stdio.h> #include<string.h> 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); }

14 常用的String Library Function
範例十: strchr( )及strstr #include<stdio.h> #include<string.h> 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); }


Download ppt "Introduction to the C Programming Language"

Similar presentations


Ads by Google