char* strchr(char*s,charc) { while(*s!='\0'&&*s!=c) { ++s; } return*s==c?s:NULL; } // strchr.c查找字符串s中首次出现字符c的位置 #include<stdio.h> #include<string.h> char* my_strchr(const char *s1, const char *s2) { const char *p = s1; const size_t len = strlen (s2); for(;(p = strchr (p, *s2))!=0;p++) { if(strncmp(p,s2,len)==0) return(char*)p; } return(0); } int main(void) { /* char string[17]; char *ptr, c = 'a'; strcpy(string, "This is a string"); ptr = strchr(string, c); //ptr = my_strchr(string, c); if (ptr) { *ptr='\0'; printf("%s\n", string); //printf("The character %c is at position: %d,ptr=%s,string=%s\n", c, ptr-string,ptr,string); } else printf("The character was not found\n"); return 0; */ /* char *ifName="atm/(0_0_32)", *pStart, *pEnd, p[16]; pStart=strchr(ifName,'('); pEnd=strchr(ifName,')'); p[0]='\0'; strncat(p,pStart+1,pEnd-pStart-1); printf("%s,pStart=%d,pEnd=%d\n",p,pStart,pEnd); */ /* char wanL2PvcName[32]; char *pStart,*pEnd,*pvcname='\0'; strncpy(wanL2PvcName, "atm0/(0_0_45)", sizeof(wanL2PvcName)-1); if ((pStart = strchr(wanL2PvcName, '(')) && (pEnd = strchr(wanL2PvcName, ')'))) { //now wanL2PvcName is "0_0_32", "0_0_45" etc. wanL2PvcName[0]='\0'; strncat(wanL2PvcName,pStart+1,pEnd-pStart-1); printf("wanL2PvcName=%s\n",wanL2PvcName); } */ char *name="ipoe_0_0_32",*pvcname; if (name != NULL) { *name = '\0'; } if(pvcname = strchr(name,'_')) { if(strcmp(pvcname+1,"0_0_32") == 0) printf("pvcname+1=%s\n\n",pvcname+1); } } //函数strstr(),可以用于在一个较长的字符串中查找匹配的字符串或字符。 //string strstr(string str,string needle) //如果找到了needle的准确位置,将从needle的前面返回一个字符串。否则返回false。如果不止一个needle,返回的字符串从出现的第一个needle开始。 //strchr和strstr()类似。 //stristr()不区分大小写,strchr区分大小写 //strrchr()是从最后出现needle的位置开始返回string值。