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.

dsPIC<br />

®<br />

Language Tools Libraries<br />

DS51456B-page 304<br />

strcpy(buf1, buf3);<br />

2004 Microchip Technology Inc.<br />

printf("buf1 after strcpy of buf3: \n\t%s\n",<br />

buf1);<br />

}<br />

Output:<br />

buf1 : We're here<br />

buf2 : Where is the time?<br />

buf3 : Why?<br />

buf1 after strcpy of buf2:<br />

Where is the time?<br />

buf1 after strcpy of buf3:<br />

Why?<br />

strcspn<br />

Description: Calculate the number of consecutive characters at the beginning of<br />

a<br />

string that are not containedin a set of characters.<br />

Include: <br />

Prototype: size_t strcspn(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 the length of the segment in s1not containing characters<br />

found in s2.<br />

Remarks: This function will determine the numberof consecutive characters<br />

from<br />

the beginning of s1that are not contained in s2.<br />

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

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

int main(void)<br />

{<br />

char str1[20] = "hello";<br />

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

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

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

int res;<br />

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

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

Ví dụ: # include / * Cho strncat, strlen * /<br />

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

int main (void)<br />

{<br />

char buf1 [50] = "Chúng tôi ở đây";<br />

char buf2 [50] = "đâu là thời điểm";<br />

char buf3 [50] = "Tại sao?";<br />

strlen (Tiếp theo)<br />

Thư viện chuẩn C với hàm toán học<br />

2004 Microchip Technology Inc DS51456B trang 307<br />

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

printf ("\ t (% d ký tự) \ n \ n", strlen (buf1));<br />

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

printf ("\ t (% d ký tự) \ n \ n", strlen (buf2));<br />

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

printf ("\ t (% d ký tự) \ n \ n \ n", strlen (buf3));<br />

strncat (buf1, buf2, 6);<br />

printf ("buf1 sau strncat 6 ký tự"<br />

"Của buf2: \ n \ t% s \ n", buf1);<br />

printf ("\ t (% d ký tự) \ n", strlen (buf1));<br />

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

strncat (buf1, buf2, 25);<br />

printf ("buf1 sau strncat 25 ký tự"<br />

"Của buf2: \ n \ t% s \ n", buf1);<br />

printf ("\ t (% d ký tự) \ n", strlen (buf1));<br />

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

strncat (buf1, buf3, 4);<br />

printf ("buf1 sau strncat của 4 nhân vật"<br />

"Của buf3: \ n \ t% s \ n", buf1);<br />

printf ("\ t (% d ký tự) \ n", strlen (buf1));<br />

}<br />

Đầu ra:<br />

buf1: Chúng tôi ở đây<br />

(10 ký tự)<br />

buf2: đâu là thời gian?<br />

(18 ký tự)<br />

buf3: Tại sao?<br />

(4 ký tự)<br />

buf1 sau strncat 6 ký tự của buf2:

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

Saved successfully!

Ooh no, something went wrong!