1 / 19

Strings

Strings. C Strings. ในภาษาซีใช้ array of character ในการเก็บสายอักขระ( string) เพิ่ม null character ‘’ ต่อท้ายอักขระตัวท้าย เป็นการ บอกจุดสิ้นสุดสตริง. เช่น char code[6];. เช่น char name[6]= “ BERRY ” ;. เช่น char str[6]= “ JAN ” ;. คำสั่งรับค่า - แสดงผลสตริง. C Strings.

dena
Télécharger la présentation

Strings

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. Strings

  2. C Strings • ในภาษาซีใช้ array of character ในการเก็บสายอักขระ(string) • เพิ่ม null character ‘\0’ ต่อท้ายอักขระตัวท้าย เป็นการบอกจุดสิ้นสุดสตริง

  3. เช่นchar code[6];

  4. เช่นchar name[6]=“BERRY”;

  5. เช่นchar str[6]=“JAN”;

  6. คำสั่งรับค่า - แสดงผลสตริง

  7. C Strings • ในภาษาซีมี standard library function เกี่ยวกับสตริงให้ใช้งาน • ที่ใช้งานบ่อยๆ ได้แก่ strlen(ชื่อตัวแปรสตริง) strcpy(ชื่อตัวแปรสตริงปลายทาง, ชื่อตัวแปรสตริงต้นทาง)

  8. ตัวอย่าง char s[5]; strcpy(s, “MWIT”); s s memory [0] [1] [2] [3] [4]

  9. Note!! • assignment operator หรือ เครื่องหมายเท่ากับ (=) ไม่สามารถใช้กำหนดค่าให้กับตัวแปรสตริงได้ ต้องใช้ฟังก์ชัน strcpy() เท่านั้น strcpy(s, “MWIT”);  S = “MWIT” ;

  10. Question Q: จะเกิดอะไรขึ้น ถ้าเราเก็บข้อความ “MahidolWittayanusorn” ลงในตัวแปร char s[10]; A: ข้อความจะถูกเก็บไว้ในตัวแปร s และจะบันทึกตัวอักษรที่เกินไปด้วย ซึ่งอาจไปบันทึกทับข้อมูลที่ถูกเก็บไว้ถัดจากตัวแปร s

  11. ตัวอย่าง ผลลัพธ์ char s[10]; int len; strcpy(s, “MWIT”); len = strlen(s); printf(“%d”, len); 4

  12. string.h • strcpy(dest_string, source_string); • int a = strlen(string); • strcat(dest_string, source_string); • int a = strcmp(string1, string2); • string1 == string2 if a == 0 • string1 < string2 if a is negative (-) • string1 > string2 if a is positive (+)

  13. โจทย์ 4 • ตรวจสอบ string ที่ผู้ใช้ป้อนเข้ามาว่าเป็น palindrome หรือไม่ เช่น level  success deed maimai

  14. Multidimensional Arrays • ตัวอย่างการประกาศ array 2 มิติ ขนาด 10 x 10 โดยเก็บเลขจำนวนเต็ม กำหนดค่าแรกและค่าสุดท้ายเป็น 13 int board[10][10]; board[0][0] = 13; board[9][9] = 13;

  15. Memory Row 0 Row 1 Row 2 Row 3 ถ้ากำหนด int b[4][3]; 0 1 2 0 1 2 3 Rows Columns

  16. Memory Row 0 Row 1 Row 2 Row 3 ถ้ากำหนด int b[2][4][3]; Columns 0 1 2 Page 0 0 1 2 3 Rows Page 1 Page 0

  17. array - initialize int a[2][3] = { 1, 2, 3, 4, 5, 6}; int a[2][3] = { { 1, 2, 3 }, {4, 5, 6 } }; int a[2][3] = { { 1, 2 }, {3, 4, 5 } }; int a[5][5] = { { 1, 2, 3 }, {4, 5, 6 } };

  18. array - initialize int a[][3] = { { 1, 2, 3 }, {4, 5, 6 } }; int a[2][] = { { 1, 2, 3 }, {4, 5, 6 } };

  19. โจทย์ 5 • เขียนโปรแกรมคำนวณหาผลบวก ลบ และผลคูณเมตริกซ์ขนาด 3 x 3 • A + B • A – B • A x B

More Related