Download - สตริง (String)
1
สตริ�ง (String)
• การิปริะกาศค่ าต�วแปริสตริ�ง• การิก�าหนดค่ าสตริ�ง • การิอ้�างอ้�งต�วอ้�กษริแต ละต�วในสตริ�ง• ฟั�งก�ชั่��นที่ �ใชั่�ในการิจั�ดการิสตริ�ง• ฟั�งก�ชั่��นในการิเปล �ยนริ$ปแบบขอ้งสตริ�ง• ฟั�งก�ชั่��นที่ �มี การิผ่ านค่ าเข�า แต ไมี มี การิส งค่ ากล�บ
2
การิปริะกาศต�วแปริสตริ�ง ริ$ปแบบ
char name_variable[size + ‘\0‘]
char * name_variable;ต�วอ้ย าง char p[9]= "I think!";
char *st;
char p[9] = "I think!";
P[0] p[1] p[2] p[3] p[4] p[5] p[6] p[7] p[8]
‘I’ ‘ ’ ‘t’ ‘h’ ‘I’ ‘n’ ‘k’ ‘!’ ‘\0’
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
#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();}
ต�วอ้ย างที่ � 1
ผ่ลริ�นโปริแกริมีM = 77a = 97
h = 104a = 97
n = 110a = 97k = 107o = 111r = 114n = 110
Mahanakorn
5
การิอ้�างอ้�งต�วอ้�กษริแต ละต�วในสตริ�งการิอ้�างแบบอ้าริ�เริย� char st[] = "Thai";
st[0] st[1] st[2] st[3] st[4]
‘T‘ ‘h‘ ‘a‘ ‘i‘ ‘\0‘
*(st) *(st+1) *(st+2) *(st+3) *(st+4)
‘T‘ ‘h‘ ‘a‘ ‘i‘ ‘\0‘
การิอ้�างแบบพอ้ยน�เตอ้ริ� char *st;
st = malloc(5*sizeof(char)); strcpy(st,”Thai”);
6
ฟั�งก�ชั่�นที่ �ใชั่�ในการิจั�ดการิสตริ�ง ฟั�งก�ชั่�นริ�บข�อ้มี$ล
scanf()int scanf(const char *format[, address, ...]);
#include <stdio.h>void main(void){ char msg[20]; printf("Enter Message : "); scanf("%s",msg);}
ต�วอ้ย างการิใชั่�งาน scanf
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, ...]);
ต�วอ้ย างการิใชั่�งาน printf1. char name[] =
"Sopon";printf("%s",name);
2.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 : 45111025Please input your First Name : AkinoPlease input your Age :45
Display your Data
Your ID : 45111025Your First Name : AkinoYour 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 AfternoonString length is 14
ผ่ลการิที่�างานขอ้งโปริแกริมี
12
ฟั�งก�ชั่�นที่ �ใชั่�จั�ดการิสตริ�งโดยเฉพาะ
ฟั�งก�ชั่�นหาค่วามียาวสตริ�ง strlen() size_t strlen(const char *s)
จัะใชั่�ฟั�งก�ชั่��นเหล าน -จัะต�อ้ง load ไฟัล� string.h ด�วย #include <string.h>
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
ผ่ลล�พธ์�จัากการิเปริ ยบเที่ ยบมี ด�งน -int strcmp(const char *s1, const char *s2)
s1 < s2 ค่�าน้�อยกว่�า 0s1 > s2 ค่�ามากกว่�า 0s1 = s2 ค่�าเท่�าก บ 0
ฟั�งก�ชั่�นเปริ ยบเที่ ยบสตริ�ง n ต�ว strcmp()
int strcmp(const char *s1, const char *s2,size_t maxlen)
15
#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);}
ต�วอ้ย างที่ � 4 strlen
Enter string : Good AfternoonString length is 14
ผ่ลการิที่�างานโปริแกริมี
16
#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();}
ต�วอ้ย างที่ � 5strcpy และ strcat
s1 : I loves2 : My Universitys3 :
Copy s1 to s3 : use strcpy()s3 : I love
Put s2 to the end of s3 : use strcat()s3 : I love My UniversityPlease any key to continue.
ผ่ลการิที่�างานโปริแกริมี
17
ต�วอ้ย างที่ � 6 strcmp
#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”);}
Enter password : 12356Enter password : gordEnter password : t567Enter password : helloEnter password : HelloPassword OK.
ผ่ลการิที่�างานโปริแกริมี
18
ต�วอ้ย างที่ � 7 strcha
#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();}
19
String : MahanakornStart in memory at 00AAPlease type character that you to find ? aFound in memory at 00ABThe first character ‘a‘ Found at 2Please any key to continue.
ผ่ลการิที่�างานขอ้งโปริแกริมี
ต�าแหน งต�วอ้�กษริขอ้งสตริ�งในหน วยค่วามีจั�าstr1 str1
+1str1+2
str1+3
str1+4
str1+5
str1+6
str1+7
‘M‘ ‘a‘ ‘h‘ ‘a‘ ‘n‘ ‘a‘ ‘k‘ ‘o‘str1+8
str1+9
str1+10
‘r‘ ‘n‘ ‘\0‘
20
ต�วอ้ย างที่ � 8
#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"); }
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 13Character 'o' Found at 14Press 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
ผ่ลการิที่�างานโปริแกริมี