C語言庫函數的實戰之一
阿新 • • 發佈:2017-10-02
定義 char include clu see dia light 實戰 obi
//-1:strtok()函數 #include<stdio.h> #include<string.h> int main(void) { char buf[]="hello#world#today";//即將被分割的字符串 char *temp = strtok(buf,"#"); while(temp) { printf("%s ",temp); temp = strtok(NULL,"#"); } printf("\n"); return 0; }
//-2:strrchr() #include <string.h> #include <stdio.h> void main() { char * pCh = "www.inkcool.com"; char * pFind = strrchr(pCh, ‘.‘);//找出點.最後一次出現在pCH的位置 if ( pFind != NULL) { printf("%s", pFind); //可以直接printf(pFind);printf("/n");左邊的表達式是合二為一的表達方法; } printf("\n"); }
//-3:strpbrk():返回兩個字符串中首個相同字符的位置 #include<stdio.h> #include<string.h> int main(void){ char* s1 = "http://see.xidian.edu.cn/cpp/u/xitong/";//在C語言中,用指針形式來定義未知長度的字符串 char* s2 = "see"; char* p = strpbrk(s1,s2); if(p){ printf("The result is: %s\n",p); }else{
//-4:strlen()返回字符串的長度 #include<stdio.h> #include<string.h> int main(void) { char* str1 = "zheng chaobing"; printf("strlen(str1)=%d",strlen(str1)); return 0; }
printf("Sorry!\n"); } return 0; }
C語言庫函數的實戰之一