งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

Operator of String Data Type

งานนำเสนอที่คล้ายกัน


งานนำเสนอเรื่อง: "Operator of String Data Type"— ใบสำเนางานนำเสนอ:

1 Operator of String Data Type
Laboratory I Operator of String Data Type

2 String รูปแบบการกำหนดตัวแปรสตริง ตัวอย่าง
Data Type (ชนิดข้อมูล) ที่เป็นสายอักขระเรียงต่อเนื่องกันโดยมีจุดสิ้นสุดที่ตัวอักษรพิเศษที่เรียกว่า Null Character แสดงด้วยโค้ด ‘\0’ รูปแบบการกำหนดตัวแปรสตริง char ชื่อตัวแปรสตริง[จำนวนตัวอักษร]; ตัวอย่าง char str[80]; char msg[] = “Welcome to Silpakorn University”

3 gets gets(ตัวแปรสตริง); ตัวอย่าง
เป็นฟังก์ชันใช้สำหรับรับค่าตัวแปรสตริงจากคีย์บอร์ด รูปแบบการใช้ gets(ตัวแปรสตริง); ตัวอย่าง char str[80]; printf(“Enter : ”); gets(str); printf(“result is \n%s\n”,str); Enter: cat is white Result is cat is white

4 strcpy strcpy(สตริงปลายทาง, สตริงต้นทาง); ตัวอย่าง
เป็นฟังก์ชันใช้สำหรับคัดลอกค่าสตริงต้นทางไปยังตัวแปรสตริงปลายทาง รูปแบบการใช้ strcpy(สตริงปลายทาง, สตริงต้นทาง); ตัวอย่าง char str[80]; strcpy(str, “trains is very slow”); printf(“result is \n%s\n”,str); Result is trains is very slow

5 strcat strcat(สตริงปลายทาง, สตริงต้นทาง); ตัวอย่าง
เป็นฟังก์ชันนำสตริงต้นทางไปต่อท้ายสตริงปลายทาง รูปแบบการใช้ strcat(สตริงปลายทาง, สตริงต้นทาง); ตัวอย่าง char str[80]; char *postfix = “korn”; strcpy(str, “Silpa”); Strcat(str, postfix); printf(“result is %s\n”,str); Result is Silpakorn

6 strcmp strcat(สตริง s1, สตริง s2); ตัวอย่าง
รูปแบบการใช้ strcat(สตริง s1, สตริง s2); ตัวอย่าง char str[80] = “abcd”; int x = strcmp(str, “abcd”); printf(“result is %d\n”,x); Result is 0

7 strcmp ผลลัพธ์ เป็นค่าจำนวนเต็ม
โดยถ้า s1 น้อยกว่า s2 จะได้ผลลัพธ์มีค่าน้อยกว่าศูนย์ 0 โดยถ้า s1 เท่ากับ s2 จะได้ผลลัพธ์มีค่าเท่ากับศูนย์ 0 โดยถ้า s1 มากกว่า s2 จะได้ผลลัพธ์มีค่ามากกว่าศูนย์ 0 ตัวอย่าง printf(“result is %d\n”, strcmp(“ABCD”, “abcd”)); printf(“result is %d\n”, strcmp(“abcd”, “abcd”)); printf(“result is %d\n”, strcmp(“abcd”, “ABCD”));

8 strlen strlen(สตริง); ตัวอย่าง เป็นฟังก์ชันไว้สำหรับหาความยาวของสตริง
รูปแบบการใช้ strlen(สตริง); ตัวอย่าง char str[80] = “abcd”; int x = strlen(str); printf(“result is %d\n”,x); Result is 4

9 strtok เป็นฟังก์ชันสำหรับแยกสตริงด้วยการกำหนดตัวแยกเป็นสตริง
ตัวอย่างเช่น char a = “ab-cdefg-hijk”; ถ้าเราให้ตัวแยกเป็น “–” จะได้ว่าสตริงข้างต้นมี 3 สตริงย่อยคือ สตริงย่อยตัวที่ 1 คือ ab สตริงย่อยตัวที่ 2 คือ cdef สตริงย่อยตัวที่ 3 คือ hijk

10 strtok strtok(สตริง s1, สตริง s2); ขั้นตอนการทำงานของฟังก์ชัน
รูปแบบการใช้ strtok(สตริง s1, สตริง s2); a1 a2 s2 s1 ขั้นตอนการทำงานของฟังก์ชัน หาสตริง s2 ที่ปรากฏใน s1 ถ้าเจอแทน s2 ด้วย null character คืนค่า s1 /0

11 strtok วิธีการใช้งานจริงจะต้องมี 2 ขั้นคือ
strtok(str, “-”); strtok(NULL, “-”);

12 ตัวอย่างโปรแกรมใช้ strtok
#include <stdio.h> #include <conio.h> #include <string.h> void main(){ char str[80]; char *p; clrscr(); strcpy(str, “cat&dog”); p = strtok(str, “&”); printf(“%s\n”,p); getch(); }

13 atoi atoi(สตริงในรูปตัวเลข); ตัวอย่าง
เป็นฟังก์ชันสำหรับแปลงค่าข้อความสตริงที่อยูในรูปตัวเลขให้เป็นค่าตัวเลขที่สามารถนำมาคำนวณได้ ถ้าไม่สามารถแปลงได้จะคืนค่าศูนย์มาให้ เช่น “abc” เป็นต้น รูปแบบการใช้ atoi(สตริงในรูปตัวเลข); ตัวอย่าง int x = atoi(“123”); printf(“result is %d\n”,x); Result is 123

14 ตัวอย่างโปรแกรมใช้ atoi
#include <stdlib.h> #include <stdio.h> #include <string.h> int main(void){ int n; char *str; strcpy(str," "); n = atoi(str); printf("string = %s integer = %d\n", str, n); strcpy(str,"123"); n = atoi(str); strcpy(str,"T123"); n = atoi(str); strcpy(str,"1T23"); n = atoi(str); strcpy(str,"123T"); n = atoi(str); return 0; }


ดาวน์โหลด ppt Operator of String Data Type

งานนำเสนอที่คล้ายกัน


Ads by Google