函數名: strchr
頭檔:<string.h>
函數原型: char *strchr(const char *str, char c);
功能: 尋找字串中第一個出現的指定字元的位置
參數: char *str 為要尋找的目標字串;
char c 為要尋找的字元;
傳回值: 成功傳回字元第一次出現的位置;失敗回傳NULL;
程式範例: 尋找字串string中指定字元c的首次出現的位置
#include<string.h>#include<stdio.h>intmain(void){charstring[15];//定義字元陣列char*ptr,c='c';strcpy(string,www.dotcpp.com); //複製字串ptr=strchr( string,c);//找出字元出現的第一個位置if(ptr){printf(Thecharacter%cisatposition:%dn,c,ptr-string);}else{printf(Thecharacterwasnotfoundn);}return0 ;}
運行結果:
Thecharactercisatposition:7