1 / 14

ข้อมูลชนิดสตริง String บทที่ 8

ข้อมูลชนิดสตริง String บทที่ 8. ใช้สำหรับ Turbo C++ Version 3. สตริงในภาษาซี.

Télécharger la présentation

ข้อมูลชนิดสตริง String บทที่ 8

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บทที่ 8 ใช้สำหรับ Turbo C++ Version 3

  2. สตริงในภาษาซี สตริง (String) เป็นลักษณะของข้อมูลที่มาจากกลุ่มของตัวอักษร หรือเป็น Array of Character นำมาใช้ในการเก็บข้อมูลประเภท ชื่อ,นามสกุล, ที่อยู่ หรือเบอร์โทรศัพท์ ในภาษาซีไม่มีโครงสร้างของตัวแปรชนิดนี้มาให้เป็นมาตรฐาน ซึ่งเราจะต้องประยุกต์สร้างโครงสร้างของตัวแปรเอง

  3. การสร้างตัวแปรแบบสตริงการสร้างตัวแปรแบบสตริง • สตริงก็คือกลุ่มของตัวอักขระ เพราะฉะนั้นถ้าเรานำเอารูปแบบของแอเรย์ มาผนวกกับตัวอักษร เราก็จะได้กลุ่มของตัวอักษร เรียกว่า Array of Character นั่นเอง ดังตัวอย่าง • char Road[100]; • char Amphor[50]; • char *string_pointer;

  4. การกำหนดค่า ให้กับตัวแปรแบบสตริง • char FirstName[15] = {'M', 'r', '.', '', 'S', 'o', 'm', 'c', 'h', 'a', 'i', '\0'}; • char Location[] = { 'R', 'a', 'j', 'a', 'm', 'a', 'n', 'g', 'a', 'l', 'a', ' ', 'U', 'n', 'i', 'v', 'e', 's', 'i', 't', 'y', '', 'o', 'f', '', 'T', 'e', 'c', 'h', 'n', 'o', 'l', 'o', 'g', 'y', '\0' } ; • char PhoneNo[] = {'0', '2', '8', '1', '8', '8', '2', '1', '6', '-', '9', '\0' } ; • char Address[] = "RMUTT Klong6 Pathumthani" ;

  5. การการรับ และแสดงผลข้อมูล char Road[100]; printf("Please enter the road name : "); gets(Road); printf("String data is %s" , Road ); รับข้อมูล แสดงข้อมูล

  6. ตัวอย่าง โปรแกรมเก็บข้อมูลสตริงเข้าไปไว้ในตัวแปร และแสดงผลลัพธ์ #include <stdio.h> #include <conio.h> void main (void) { char str[80]; printf("Enter a string : "); gets(str); printf("You enter is : %s",str); getch(); } Enter a string : Apple Mango Orange You enter is : Apple Mango Orange

  7. ตัวอย่าง การรับข้อมูลที่เป็นสตริง และพิมพ์ข้อมูลในสตริงออกมาทีละเซลล์ #include <stdio.h> #include <conio.h> void main (void) { char str[80]; int i; printf("Enter a string (less than 80 chars) : "); gets(str); for (i=0;str[i];i++) printf("str[%d] = %c\n" , i, str[i]); getch(); } Enter a string (less than 80 chars) : I'm free. str[0] = I str[1] = ' str[2] = m str[3] = str[4] = f str[5] = r str[6] = e str[7] = e str[8] = .

  8. การส่งผ่านค่าสตริงระหว่างฟังก์ชันการส่งผ่านค่าสตริงระหว่างฟังก์ชัน • การส่งผ่านค่าสตริงในฝั่งของฟังก์ชันที่ถูกเรียกใช้งาน พารามิเตอร์ของฟังก์ชัน จะไม่ระบุขนาดของแอเรย์ เนื่องจากไม่สามารถทราบได้แน่นอนว่ามีขนาดเท่าใด ดังตัวอย่าง

  9. ตัวอย่าง การส่งผ่านค่าสตริงระหว่างฟังก์ชัน #include <stdio.h> #include <conio.h> void function1(char [] ); void main (void) { char string[20]; printf(“What is your name => "); gets(string); function1(string); getch(); } void function1(char name[]) { printf("Hello there %s!" , name ); } What is your name => Somchai Jingjing Hello there Somchai Jingjing!

  10. ตัวอย่างนี้ค่าของสตริงถูกระบุในฟังก์ชันตัวอย่างนี้ค่าของสตริงถูกระบุในฟังก์ชัน #include <stdio.h> void function2(char [] ); void main (void) { char string[20]; function2(string); printf("Hello there %s!\n" , string); getch(); } void function2(char name[]) { printf("Enter your first name => "); gets(name); } Enter your first name => Rooney Hello there Rooney!

  11. ฟังก์ชันที่ใช้งานกับข้อมูลแบบสตริงฟังก์ชันที่ใช้งานกับข้อมูลแบบสตริง • ฟังก์ชันที่ทำงานกับข้อมูลแบบสตริงมีอยู่มากมายเช่น การคัดลอก การเปรียบเทียบ การทำให้เป็นตัวอักษรเล็ก หรือใหญ่ ต่างๆ ซึ่งอยู่ใน “string.h” ก่อนนำฟังก์ชันมาใช้งาน ต้องมีการกำหนดไว้ใน include directive ก่อนเสมอคือ #include <string.h> void main() { Statement … Statement }

  12. ตัวอย่าง ฟังก์ชันที่ใช้งานกับข้อมูลแบบสตริง

  13. ตัวอย่าง การใช้งานฟังก์ชัน STRSTR : Help me!! STRCHR : Help me!! STRCPY : Help STRCMP : 0 STRCAT : Superman!! Help me!!Help STRUPR : SUPERMAN!! HELP ME!!HELP STRLWR : superman!! help me!!help #include <stdio.h> #include <string.h> void main (void) { char str1[30]="Superman!! Help me!!"; char str2[30]="Help"; char str3[30]=""; printf("STRSTR : %s\n" , strstr(str1,str2)); printf("STRCHR : %s\n" , strchr(str1,'H')); printf("STRCPY : %s\n" , strcpy(str3,str2)); printf("STRCMP : %d\n" , strcmp(str3,str2)); printf("STRCAT : %s\n" , strcat(str1,str2)); printf("STRUPR : %s\n" , strupr(str1)); printf("STRLWR : %s\n" , strlwr(str1)); getch(); }

  14. The End.

More Related