30.10.2014 Views

o_195h4n6al16jb186b1b2qs7fgssa.pdf

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

es = strncmp(buf1, buf3, 20);<br />

if (res < 0)<br />

printf("buf1 comes before buf3\n");<br />

else if (res == 0)<br />

printf("20 characters of buf1 and buf3 "<br />

"are equal\n");<br />

else<br />

printf("buf3 comes before buf1\n");<br />

}<br />

Output:<br />

buf1 : Where is the time?<br />

buf2 : Where did they go?<br />

buf3 : Why?<br />

6 characters of buf1 and buf2 are equal<br />

buf2 comes before buf1<br />

buf1 comes before buf3<br />

strncpy<br />

Description: Copy characters from the source string into the destination string,<br />

up to<br />

the specified number of characters.<br />

Include: <br />

Prototype: char *strncpy(char *s1, const char *s2, size_t n);<br />

Arguments: s1 destination string to copy to<br />

s2 source string to copy from<br />

n number of characters to copy<br />

Return Value: Returns a pointer to the destination string.<br />

Remarks: Copies ncharacters from the source string to the destination string. If<br />

the source string is less than ncharacters, the destination is filled with<br />

null characters to total ncharacters. If ncharacters were copied and no<br />

null character was found then the destination string will not be<br />

null-terminated. If the strings overlap, the behavior is undefined.<br />

Example: #include /* forstrncpy, strlen */<br />

#include /* for printf */<br />

int main(void)<br />

{<br />

char buf1[50] = "We're here";<br />

char buf2[50] = "Where is the time?";<br />

char buf3[50] = "Why?";<br />

char buf4[7] = "Where?";<br />

Thư viện Công cụ Ngôn ngữ<br />

DS51456B trang 312<br />

printf ("\ n");<br />

2004 Microchip Technology Inc<br />

printf ("strpbrk (\"% s \ ", \"% s \ ") \ n", str1, str3);<br />

ptr = strpbrk (str1, str3);<br />

if (ptr! = NULL)<br />

{<br />

res = ptr - str1 + 1;<br />

printf ("phù hợp với tìm thấy ở vị trí% d \ n", độ phân giải);<br />

}<br />

khác<br />

printf ("matchnot tìm thấy \ n");<br />

}<br />

Đầu ra:<br />

strpbrk ("thời gian gì isit?", "xyz")<br />

phù hợp không tìm thấy<br />

strpbrk ("thời gian gì isit?", "EOU?")<br />

phù hợp với tìm thấy ở vị trí 9<br />

strrchr<br />

Mô tả: Tìm kiếm sự xuất hiện cuối cùng của một nhân vật được quy định trong<br />

một chuỗi.<br />

Bao gồm:<br />

Prototype: char * strrchr (const char * s, int c);<br />

Đối số: s con trỏ đến chuỗi cần tìm kiếm<br />

c nhân vật để tìm kiếm<br />

Quay trở lại giá trị: Trả về một con trỏ tới các nhân vật nếu tìm thấy; nếu không<br />

trả về một null<br />

con trỏ.<br />

Ghi chú: Chức năng tìm kiếm chuỗi, bao gồm cả việc chấm dứt vô<br />

nhân vật, để tìm sự xuất hiện cuối cùng của nhân vật c.<br />

Ví dụ: # include / * Cho strrchr, NULL * /<br />

# include / * Cho printf * /<br />

int main (void)<br />

{<br />

char buf1 [50] = "Bây giờ là mấy?";<br />

char ch1 = 'm', CH2 = 'y';<br />

char * ptr;<br />

res int;<br />

printf ("buf1:% s \ n \ n", buf1);<br />

ptr = strrchr (buf1, ch1);

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!