1 / 23

สตริง (String)

สตริง (String). การประกาศค่าตัวแปรสตริง การกำหนดค่าสตริง การอ้างอิงตัวอักษรแต่ละตัวในสตริง ฟังก์ชั่นที่ใช้ในการจัดการสตริง ฟังก์ชั่นในการเปลี่ยนรูปแบบของสตริง ฟังก์ชั่นที่มีการผ่านค่าเข้า แต่ไม่มีการส่งค่ากลับ. การประกาศตัวแปรสตริง. รูปแบบ char name_variable[size + ‘‘]

alodie
Télécharger la présentation

สตริง (String)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. สตริง (String) • การประกาศค่าตัวแปรสตริง • การกำหนดค่าสตริง • การอ้างอิงตัวอักษรแต่ละตัวในสตริง • ฟังก์ชั่นที่ใช้ในการจัดการสตริง • ฟังก์ชั่นในการเปลี่ยนรูปแบบของสตริง • ฟังก์ชั่นที่มีการผ่านค่าเข้า แต่ไม่มีการส่งค่ากลับ

  2. การประกาศตัวแปรสตริง รูปแบบ char name_variable[size + ‘\0‘] char * name_variable; ตัวอย่าง char p[9]= "I think!"; char *st; char p[9] = "I think!";

  3. การกำหนดค่าสตริง ตัวอย่าง char var[10] = "Hello"; char var[] = "Hello"; char *var = "Hello"; char var[] = {‘H‘,‘e‘,‘l‘,‘l‘,‘o‘,‘\0‘}; ตัวอย่างที่ผิดพลาด char gh[5] = “MMMMMM”; Too many Initializers

  4. ตัวอย่างที่ 1 #include <stdio.h> #include <conio.h> void main(void) { char string[] = “Mahanakorn”; int a = 0; while (string[a] != ‘\0’) { printf(“%c = %d\n”,string[a], string[a]); a++; } printf (“\n%s”,string); getch(); } ผลรันโปรแกรม M = 77 a = 97 h = 104 a = 97 n = 110 a = 97 k = 107 o = 111 r = 114 n = 110 Mahanakorn

  5. การอ้างอิงตัวอักษรแต่ละตัวในสตริงการอ้างอิงตัวอักษรแต่ละตัวในสตริง • การอ้างแบบอาร์เรย์char st[] = "Thai"; • การอ้างแบบพอยน์เตอร์ char *st; • st = malloc(5*sizeof(char)); • strcpy(st,”Thai”);

  6. ฟังก์ชันที่ใช้ในการจัดการสตริงฟังก์ชันที่ใช้ในการจัดการสตริง ฟังก์ชันรับข้อมูล scanf() int scanf(const char *format[, address, ...]); ตัวอย่างการใช้งาน scanf #include <stdio.h> void main(void) { char msg[20]; printf("Enter Message : "); scanf("%s",msg); }

  7. gets() char *gets (char *s) ตัวอย่างการใช้งาน gets #include <stdio.h> #include <stdlib.h> void main(void) { char *msg; msg = malloc(30*sizeof(char)); printf("Enter Message : "); gets(msg); }

  8. ฟังก์ชันแสดงผลข้อมูล printf() int printf( const char *format [, argument, ...]); ตัวอย่างการใช้งาน printf • 1. char name[] = "Sopon"; • printf("%s",name); • printf("%s","sopon"); • 3. printf("sopon");

  9. puts() int puts(const char *s) ตัวอย่างการใช้งาน puts #include <stdio.h> void main (void) { char ppp[20]= "Technology"; puts(ppp); }

  10. ตัวอย่างที่ 2 #include <stdio.h> #include <conio.h> #include <stdlib.h> void main (void) { char id[9],*fname; int age; clrscr(); fname = malloc(30); printf("Input your Data\n\n"); printf("Please input your ID: "); scanf("%s",id); fflush(stdin); printf("Please input your First Name : "); gets(fname); printf("Please input your Age :"); scanf("%d",&age); printf("\nDisplay your Data\n\n"); printf("Your ID : %s\n",id); printf("Your First Name : %s\n",fname); printf("Your Age : %d\n",age); getch(); } ผลการทำงานโปรแกรม Input your Data Please input your ID : 45111025 Please input your First Name : Akino Please input your Age :45 Display your Data Your ID : 45111025 Your First Name : Akino Your Age : 45

  11. ตัวอย่างที่ 3 #include <stdio.h> #include <conio.h> #include <malloc.h> void main(void) { char *message; int len=0; clrscr(); message=malloc(sizeof(char)*256); if(message != NULL) { printf("Enter string : "); gets(message); while(message[len] != ‘\0‘) len++; printf("String length is %d", len); getch(); } else printf("Out of Memory\n"); free(message); } ผลการทำงานของโปรแกรม Enter string : Good Afternoon String length is 14

  12. ฟังก์ชันที่ใช้จัดการสตริงโดยเฉพาะฟังก์ชันที่ใช้จัดการสตริงโดยเฉพาะ จะใช้ฟังก์ชั่นเหล่านี้จะต้อง loadไฟล์ string.hด้วย #include <string.h> ฟังก์ชันหาความยาวสตริง strlen() size_t strlen(const char *s) Int len;char message[5]=“Hello”; len = strlen(message); len จะมีค่าเท่ากับ 5 ฟังก์ชันคัดลอกสตริง strcpy() char *strcpy(char *dest, const char *src) ฟังก์ชันคัดลอกสตริงจำนวน n ตัว strncpy() char *strncpy(char *dest, const char *src, size_t maxlen)

  13. ฟังก์ชันต่อสตริง strcat() char *strcat(char *dest, const char *src) ฟังก์ชันต่อสตริงจำนวน n ตัว strncat() char *strncat(char *dest, const char *src, size_t n) ฟังก์ชันเปรียบเทียบสตริง strcmp() int strcmp(const char *s1, const char *s2) ฟังก์ชันในการค้นหาตัวอักษรในสตริง strchr() char *strchr(const char *s, int c);

  14. ผลลัพธ์จากการเปรียบเทียบมีดังนี้ผลลัพธ์จากการเปรียบเทียบมีดังนี้ ฟังก์ชันเปรียบเทียบสตริง n ตัว strcmp() int strcmp(const char *s1, const char *s2,size_t maxlen)

  15. ตัวอย่างที่ 4 strlen #include <stdio.h> #include <conio.h> #include <malloc.h> #include <string.h> void main(void) { char *message; int len; clrscr(); message = malloc(sizeof(char)*256); if(message != NULL) { printf("Enter string : "); gets(message); len = strlen(message); printf("String length is %d", len); getch(); } else printf("Out of Memory\n"); free(message); } ผลการทำงานโปรแกรม Enter string : Good Afternoon String length is 14

  16. ผลการทำงานโปรแกรม ตัวอย่างที่ 5 strcpy และ strcat s1 : I love s2 : My University s3 : Copy s1 to s3 : use strcpy() s3 : I love Put s2 to the end of s3 : use strcat() s3 : I love My University Please any key to continue. #include <stdio.h> #include <conio.h> #include <string.h> void main(void) { char s1[15] = “I love “; char s2[15] = “My University”; char s3[30]=””; clrscr(); printf(“s1 : %s”, s1); printf(“\ns2 : %s”, s2); printf(“\ns3 : %s”, s3); printf(“\n\nCopy s1 to s3 : use strcpy() \n”); strcpy(s3,s1); printf(“s3 : %s\n”, s3); printf(“\n\nPut s2 to the end of s3 : use strcat()\n”); strcat(s3, s2); printf(“s3 : %s\n”, s3); printf(“Please any key to continue. \n”); getch(); }

  17. #include <conio.h> #include <stdio.h> #include <string.h> #include <malloc.h> void main(void) { char *password="Hello"; char *psw; clrscr(); psw = malloc(sizeof(char)*257); if(psw != NULL) { printf("Enter password : "); gets(psw); while(strcmp(psw,password)) { printf(“Enter password : “); gets(psw); } printf(“Password OK.\n”); } else printf(“Out of Memory.\n”); } ตัวอย่างที่ 6strcmp ผลการทำงานโปรแกรม Enter password : 12356 Enter password : gord Enter password : t567 Enter password : hello Enter password : Hello Password OK.

  18. #include <stdio.h> #include <conio.h> #include <string.h> #include <malloc.h> void main(void) { char *str1 = "Mahanakorn"; char *ptr; char ch; clrscr(); ptr = malloc(sizeof(char)*257); if(ptr !=NULL) { printf("String : %s\nStart in memory at %p\n\n", str1,str1); printf("Please type character that you want to find? "); scanf("%c", &ch); ptr = strchr(str1, ch); if(ptr != NULL) { printf("\nFound in memory at %p\n",ptr); printf("The first character‘%c’Found at %d\n\n", ch,ptr-str1+1); } else printf("Not found character ‘%c’ in string\n", ch); } else printf("Out of Memory.\n"); printf("Please any key to continue.\n"); getch(); } ตัวอย่างที่ 7strcha

  19. ผลการทำงานของโปรแกรม String : Mahanakorn Start in memory at 00AA Please type character that you to find ? a Found in memory at 00AB The first character ‘a‘ Found at 2 Please any key to continue. ตำแหน่งตัวอักษรของสตริงในหน่วยความจำ

  20. #include <stdio.h> #include <conio.h> #include <malloc.h> void main(void) { char *str1, *ptr; char ch; int len=0,check=0; clrscr(); ptr = malloc(sizeof(char)*257); str1 = malloc(sizeof(char)*257); if((ptr !=NULL) && (str1 != NULL)) { printf("Enter Source String : "); gets(str1); printf("String : %s\nStart in memory at %p\n\n", str1,str1); printf("Please type character that you want to find? "); scanf("%c", &ch); while(str1[len] != ‘\0’) { if(str1[len] == ch) { printf("Character ‘%c’ Found at %d \n\n", ch,len+1); check=1; } len++; } if(check == 0) printf("Not found character ‘%c’ in string\n", ch); } else printf("Out of Memory.\n"); printf("Press any key to continue.\n"); } ตัวอย่างที่ 8

  21. ผลการทำงานของโปรแกรม Enter Source String : We see the moon at nigth. String : We see the moon at night. Start in memory at 0948 Please type character that you want to find ? o Character 'o' Found at 13 Character 'o' Found at 14 Press any key to continue.

  22. ฟังก์ชันที่เปลี่ยนรูปแบบของสตริงฟังก์ชันที่เปลี่ยนรูปแบบของสตริง • • atoi( ) • int atoi(const char *s) • • atol( ) • long atol(const char *s) • atof( ) • double atof(const char *s)

  23. ตัวอย่างที่ 9 #include <stdlib.h> #include <string.h> #include <stdio.h> #include <conio.h> void main(void) { double pwd; char *pwd1="24318",*pwd2="36471",*pwd3; clrscr(); pwd3 = malloc(20,sizeof(char)); pwd3 = strcpy(pwd3,pwd1); pwd3 = strcat(pwd3,pwd2); pwd = atof(pwd3); printf("string = %s \nconverted to double = %.2lf\n", pwd3, pwd); getch(); } ผลการทำงานโปรแกรม string = 2431836471 converted to double = 2431836471.00

More Related