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.

s2 second string<br />

n number of characters to compare<br />

Return Value: Returns a positive number if s1is greater than s2, zero if s1is<br />

equal to<br />

s2, or a negative number if s1is less than s2.<br />

Remarks: strncmpreturns a value based on the first character that differs<br />

between s1and s2. Characters that follow a null character are not<br />

compared.<br />

Example: #include /* for strncmp */<br />

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

int main(void)<br />

{<br />

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

char buf2[50] = "Where did they go?";<br />

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

int res;<br />

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

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

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

res = strncmp(buf1, buf2, 6);<br />

if (res < 0)<br />

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

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

printf("6 charactersof buf1 and buf2 "<br />

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

else<br />

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

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

res = strncmp(buf1, buf2, 20);<br />

if (res < 0)<br />

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

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

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

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

else<br />

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

Standard C Libraries with Math Functions<br />

2004 Microchip Technology Inc. DS51456B-page 309<br />

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

ký tự). strlenshows 4 nhân vật vì nó dừng lại khi nó<br />

đạt các ký tự null đầu tiên.<br />

Trong ví dụ thứ tư, kể từ khi buf4is chỉ có 7 nhân vật strncpyuses 2<br />

ký tự null bổ sung thay thế 9 ký tự đầu tiên của buf1. Các<br />

kết quả của buf1is 6 ký tự ("Ở đâu?") tiếp theo là 3 ký tự null,<br />

tiếp theo là 9 ký tự ("thời gian?"), tiếp theo là 32 ký tự null.<br />

strpbrk<br />

Mô tả: Tìm kiếm một chuỗi cho sự xuất hiện đầu tiên của một nhân vật từ một<br />

quy định<br />

tập hợp các ký tự.<br />

Bao gồm:<br />

Prototype: char * strpbrk (const char * s1, const char * s2);<br />

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

s2 con trỏ đến ký tự để tìm kiếm<br />

Quay trở lại giá trị: Trả về một con trỏ đến các nhân vật xuất hiện trong s1if tìm<br />

thấy; nếu không,<br />

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

Bình luận: Chức năng này sẽ tìm kiếm s1for sự xuất hiện đầu tiên của một nhân<br />

vật<br />

chứa trong s2.<br />

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

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

int main (void)<br />

{<br />

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

char str2 [20] = "xyz";<br />

char str3 [20] = "EOU?";<br />

char * ptr;<br />

res int;<br />

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

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

if (ptr! = NULL)<br />

{<br />

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

printf ("phù hợp với vị trí foundat% d \ n", độ phân giải);<br />

}<br />

khác<br />

printf ("phù hợp không tìm thấy \ n");<br />

strncpy (Tiếp theo)<br />

dsPIC<br />

®

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

Saved successfully!

Ooh no, something went wrong!