สตริง/สายอักขระ

advertisement
EECP0110 C Language
Computer Programming
C String
Contents
Computer Engineering
1
Basic of String in C Language
2
กำหนดค่ำเริม
่ ต้นให้กบั ตัวแปร String
3
String Input Functions
4
String Output Functions
5
String Library Functions
Mahanakorn University of Technology.
Basic of String in C Language
 String ในภาษา C นั้นจะใช้ Array ของ Character มาสร้างเป็ น
ข้อความ แต่จะมีอะไรที่แตกต่างจาก Array ของ Character คือจะมี null
Character(\0) เป็ นตัวปิ ดท้ายข้อความ
char string[20]=“Initial Message”;
[0]
[1]
I
n
[4]
i
t
i
[9]
a
l
M
e
s
s
a
g
[14]
[15]
e
\0
[19]
?
?
?
 ข้อมูลที่อยูห่ ลัง null character(\0) จะถูกมองข้าม
 ความยาวของข้อความคือ 15 ตัวอักษร
Computer Engineering
Mahanakorn University of Technology.
?
Basic of String in C Language
 String vs. Array of char
String
char color[] = “blue”;
char color[] = {‘b’, ‘l’, ‘u’, ‘e’,‘\0’};
Array of char
char color[] = {‘b’, ‘l’, ‘u’, ‘e’};
Computer Engineering
Mahanakorn University of Technology.
Basic of String in C Language
 การประกาศตัวแปร String ในรู ปแบบชนิ ด char*
char
month[] = “January”;
สร้าง array ขนาด 5
char
*monthPtr = “January”;
สร้างตัวแปร pointer ที่ช้ ีไปที่ string
“January” ในหน่วยความจา
Computer Engineering
Mahanakorn University of Technology.
กำหนดค่ ำเริ่มต้ นให้ กบั ตัวแปร String
 การกาหนดแบบระบุความยาวตัวอักษรใน String
char msg[10] = “Computer”;
char msg[10] = {‘C’,’o’,’m’,’p’,’u’,’t’,’e’,’r’,’\0’};
 การกาหนดแบบไม่ได้กาหนดความยาวของตัวอักษรใน String
char msg[ ] = “Computer”;
char *msg = “Computer";
char msg[] = {‘C’,’o’,’m’,’p’.’u’,’t’,’e’,’r’,’\0’};
Computer Engineering
Mahanakorn University of Technology.
กำร Copy String
char str1[10]=“Computer”;
char str2[30];
str2=str1;
str2=“Computer”;
ไม่สามารถกาหนดค่าของตัวแปร String…
...ส่ งแก่ตว
ั แปรตัวอื่นได้
Computer Engineering
Mahanakorn University of Technology.
String Input Functions
 scanf
scanf( “%s”, msg );
scanf จะอ่านค่าของสตริ งที่ user ป้ อนให้ไปเรื่ อยๆจนกว่า
จะได้รับค่า space, newline หรื อ
end-of-file character
Computer Engineering
Mahanakorn University of Technology.
String Input Functions
 หำกค่ ำสตริงที่อ่ำนมำมีขนำดตัวอักษรยำวกว่ ำขนาดของ
ตัวแปร array ที่จะจัดเก็บ จะเกิดผลอย่ ำงไร
char msg[10];
printf(“Enter Message : “);
scanf(“%s”, msg);
Computer Engineering
?
Mahanakorn University of Technology.
String Input Functions
 กำรป้ องกันไม่ ให้ อ่ำนค่ ำตัวอักษรเกินกว่ ำขนำดของสตริงที่
กำหนด ทำได้ โดยกำรระบุขนำดของตัวอักษรทีจ่ ะรับ
char msg[10];
printf(“Enter Massage : “);
scanf(“%9s”, msg);
Computer Engineering
Mahanakorn University of Technology.
String Input Functions
 Edit set ( %[…] ) คือ กลุ่มเครื่ องหมายที่ใช้ในการกาหนด
รู ปแบบการอ่าน string โดย scanf จะเลือกอ่านตัวอักษรที่
เหมือนกับตัวอักษรที่อยูใ่ น […] ของ edit set เท่านั้น
char msg[10];
printf(“Enter money : “);
scanf(“%9[1234567890,.$]s”,msg);
 ตย. การอ่านค่าเงิน สกุลดอลล่าร์ เท่านั้น
Computer Engineering
Mahanakorn University of Technology.
String Input Functions
 การกาหนดค่าใน Edit set ( %[…] ) ให้ยกเว้นการ
อ่านค่าตัวอักษรบางตัว ทาได้โดยการ ใส่ เครื่ องหมาย ( ^ ) เช่น
ตัวอย่าง กาหนดให้อ่านตัวอักษรทั้งบรรทัด ยกเว้น newline
char msg[100];
printf(“Enter message : “);
scanf(“%81[^\n]s”,msg);
 จากตัวอย่าง scanf จะอ่านตัวอักษรจนกว่าจะพบ newline
Computer Engineering
character
จึงหยุดอ่าน
Mahanakorn University of Technology.
String Output Functions
 printf
printf( “%s”, msg );

รู ปแบบการจัดรู ปแบบการพิมพ์ string
printf( “%flagprecisions”, stringที่จะพิมพ์ );
Left-justify
Computer Engineering
Mahanakorn University of Technology.
String Output Functions
printf(“|%+30s|”,”Computer”);
Output:
printf(“|%-30s|”,”Computer”);
Output:
Computer Engineering
Mahanakorn University of Technology.
String Output Functions
printf(“|%-20.15s|”,”12345678901234567890”);
Output:
กาหนดระยะพิมพ์ 20 ตาแหน่ง แต่ให้
พิมพ์ 15 ตัวอักษร
Computer Engineering
Mahanakorn University of Technology.
Standard Input/Output Library Functions
Computer Engineering
Mahanakorn University of Technology.
char *gets (char *s);
#include<stdio.h>
void main()
{
char name[20] = “”;
char question[20] = “What is your name?”;
puts(question);
gets(name);
printf(“You are %s\n”, name);
}
Computer Engineering
Mahanakorn University of Technology.
char *gets (char *s);
Computer Engineering
Mahanakorn University of Technology.
int puts(const char *s)
Computer Engineering
Mahanakorn University of Technology.
โปรแกรมใช้ sprintf พิมพ์ ข้อมูลไปเก็บใน array
Computer Engineering
Mahanakorn University of Technology.
String Library Functions
#include
Computer Engineering
<string.h>
Mahanakorn University of Technology.
String Length
int strlen( const char string[] );
strlen คืนค่า ขนำดควำมยำวของสตริง
หรื อ จานวนตัวอักษรในสตริ ง ไม่นบั รวม null
character
ถ้าสตริ งไม่มีตวั อักษรเลย จะคืนค่าศูนย์
Computer Engineering
Mahanakorn University of Technology.
String Length
#include<stdio.h>
#include<string.h>
void main()
{
char buff[20] = “”;
int num;
strcpy(buff,“What happen?”);
num = strlen(buff);
printf(“String contains %d character”,num);
}
Computer Engineering
Mahanakorn University of Technology.
String Length
#include<stdio.h>
#include<string.h>
void main()
{
char buff[20] = “”;
strcpy(buff,“What happen?”);
printf(“String contains %d character”, strlen(buff));
}
Computer Engineering
Mahanakorn University of Technology.
#include <stdio.h>
#include <conio.h>
#include <malloc.h>
#include <string.h>
void main(void)
{
char *message;
int len;
}
ตัวอย่ างคาสั่ง strlen
ผลกำรทำงำนโปรแกรม
Enter string : Good Afternoon
String length is 14
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);
Computer Engineering
Mahanakorn University of Technology.
String Copy
 ไม่สามารถกาหนดค่า String โดยใช้
assignment statement เช่น
char str1[] = ”Test String”;
char str2[12];
str2 = str1;
str2 = ”Test String”;
Computer Engineering
Mahanakorn University of Technology.
String Copy
char *strcpy( char *Destination, const char *Source );
Copies the C string pointed by Source into the array
pointed by Destination, including the terminating null
character
char * strncpy ( char * Destination, const char * Source, size_t num );
Copies the first num characters of Source to
Destination. If the end of the source C string (which is
signaled by a null-character) is found before num
characters have been copied, destination is padded with
zeros until a total of num characters have been written to
it.
Computer Engineering
Mahanakorn University of Technology.
String Copy
 การ Copy
จะเป็ นการ copy ตัวอักษรในสตริ ง
ตั้งแต่ตวั แรกไปเรื่ อยๆ จนกวา่
จะพบ ‘/0’ จึงหยุด ทาการ copy
Computer Engineering
Mahanakorn University of Technology.
strcpy
Computer Engineering
Mahanakorn University of Technology.
strncpy
Computer Engineering
Mahanakorn University of Technology.
Computer Engineering
Mahanakorn University of Technology.
String Concatenate
char * strcat ( char * destination, const char * source );
char * strncat ( char * destination, const char * source,size_t num );
strcat และ strncat
นาสตริ งชุดที่หนึ่งไปต่อท้าย สตริ งชุดที่สอง
โดยตัวอักษรตัวแรกของสตริ งชุดที่สอง จะทับ null
character ของ สตริ งชุดที่หนึ่ง
คืนค่า address ของ pointer ที่ช้ ี
สตริ งผลลัพธ์ (address ของสตริ งชุดที่หนึ่ง)
Computer Engineering
Mahanakorn University of Technology.
String Concatenate
Computer Engineering
Mahanakorn University of Technology.
String Compare
int strcmp ( const char * str1, const char * str2 );
Compares the C string str1 to the C string str2.
This function starts comparing the first character of each string. If
they are equal to each other, it continues with the following pairs
until the characters differ or until a terminanting null-character is
reached.
int strncmp ( const char * str1, const char * str2, size_t num );
Compares up to num characters of the C string str1 to those of
the C string str2.
This function starts comparing the first character of each string. If
they are equal to each other, it continues with the following pairs
until the characters differ, until a terminating null-character is
reached, or until num characters match in both strings, whichever
happens first.
Computer Engineering
Mahanakorn University of Technology.
ผลลัพธ์ จำกกำรเปรียบเทียบมีดังนี้
int strcmp ( const char * str1, const char * str2 );
int strncmp ( const char * str1, const char * str2, size_t num );
str1 < str2
ค่าน้ อยกว่า 0
str1 > str2
ค่ามากกว่า 0
str1 = str2
ค่าเท่ากับ 0
Computer Engineering
Mahanakorn University of Technology.
String Compare
strcmp(s1,s2);
Computer Engineering
Mahanakorn University of Technology.
String Compare
strcmp(s1,s2);
Computer Engineering
Mahanakorn University of Technology.
String Compare
strcmp(s1,s2);
Computer Engineering
Mahanakorn University of Technology.
ตัวอย่างการเขียนคาสัง่ strcmp เพื่อเปรี ยบเทียบ
Computer Engineering
Mahanakorn University of Technology.
ตัวอย่างการเขียนคาสัง่ strcmp เพื่อเปรี ยบเทียบ
Computer Engineering
Mahanakorn University of Technology.
ตัวอย่ ำงผลลัพธ์ ของกำรเปรียบเทียบโดยใช้ Strncmp
strncmp(string1,string2,Size);
Computer Engineering
Mahanakorn University of Technology.
String Conversion Functions
int atoi ( const char * str ); // Convert string to integer
Parses the C string str interpreting its content as an integral
number, which is returned as an int value.
double atof ( const char * str ); // Convert string to double
Parses the C string str interpreting its content as a floating point
number and returns its value as a double.
long atol ( const char * str ); // Convert string to long
Parses the C string str interpreting its content as an integral
number, which is returned as a long int value.
Computer Engineering
Mahanakorn University of Technology.
Computer Engineering
Mahanakorn University of Technology.
Search character in String
char * strchr ( char * str, int character );
Returns a pointer to the first occurrence of character in the C string str.
Computer Engineering
Mahanakorn University of Technology.
Download