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.

( 18 characters)<br />

buf1 after strncpy of 5 characters of buf3:<br />

Why?<br />

( 4 characters)<br />

buf1 after strncpy of 9 characters of buf4:<br />

Where?<br />

( 6 characters)<br />

strncpy (Continued)<br />

Standard C Libraries with Math Functions<br />

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

Explanation:<br />

Each buffer contains the string shown,followed by null characters for a<br />

length of 50. Using strlenwill find the length of the string up to but not<br />

including the first null character.<br />

In the first example, 6 characters of buf2(“Where “) replace the first 6<br />

characters of buf1("We're ") and the rest of buf1remains the same<br />

("here" plus null characters).<br />

In the second example, 18 characters replace the first 18 characters of<br />

buf1and the rest remain null characters.<br />

In the third example, 5 characters of buf3("Why?" plus a null<br />

terminating character) replace the first 5 characters of buf1. buf1now<br />

actually contains ("Why?", 1 null character, " is the time?", 32 null<br />

characters). strlenshows 4 characters because it stops when it<br />

reaches the first null character.<br />

In the fourth example, since buf4is only 7 characters strncpyuses 2<br />

additional null characters to replace the first 9 characters of buf1. The<br />

result of buf1is 6 characters ("Where?") followed by 3 null characters,<br />

followed by 9 characters ("the time?"), followed by 32 null characters.<br />

strpbrk<br />

Description: Search a string for the first occurrence of a character from a<br />

specified<br />

set of characters.<br />

Include: <br />

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

Arguments: s1 pointer to the string to be searched<br />

s2 pointer to characters to search for<br />

Return Value: Returns a pointer to the matched character in s1if found;<br />

otherwise,<br />

returns a null pointer.<br />

Remarks: This function will search s1for the first occurrence of a character<br />

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

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

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

res int;<br />

res = strspn (str1, str2);<br />

printf ("strspn (\"% s \ ", \"% s \ ") =% d \ n",<br />

str1, str2, độ phân giải);<br />

res = strspn (str1, str3);<br />

printf ("strspn (\"% s \ ", \"% s \ ") =% d \ n",<br />

str1, str3, độ phân giải);<br />

res = strspn (str1, str4);<br />

printf ("strspn (\"% s \ ", \"% s \ ") =% d \ n",<br />

str1, str4, độ phân giải);<br />

}<br />

strrchr (Tiếp theo)<br />

dsPIC<br />

®<br />

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

DS51456B trang 314<br />

Đầu ra:<br />

strspn ("động vật", "aeiounm") = 5<br />

strspn ("động vật", "aimnl") = 6<br />

strspn ("động vật", "xyz") = 0<br />

Giải thích:<br />

Trong kết quả đầu tiên, lis không s2.<br />

2004 Microchip Technology Inc<br />

Trong kết quả thứ hai, chấm dứt rỗng không có trong s2.<br />

Trong kết quả thứ ba, ais không s2, vì vậy việc so sánh dừng lại.<br />

strstr<br />

Mô tả: Tìm kiếm chuỗi xảy ra ofa đầu tiên trong chuỗi khác.<br />

Bao gồm:<br />

Prototype: char * strstr (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 chuỗi cần tìm kiếm<br />

Quay trở lại giá trị: Trả về địa chỉ của phần tử đầu tiên phù hợp với chuỗi nếu<br />

tìm thấy; nếu không, trả về một con trỏ null.<br />

Ghi chú: Chức năng này sẽ tìm thấy sự xuất hiện đầu tiên của chuỗi s2 (không<br />

bao gồm các<br />

vô terminator) trong chuỗi s1. Nếu s2points một chuỗi số không dài,<br />

s1is trả lại.<br />

Ví dụ: # include / * Cho strstr, NULL * /

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

Saved successfully!

Ooh no, something went wrong!