C語言index()函數(shù):查找字符串并返回首次出現(xiàn)的位置
相關(guān)函數(shù):rindex, srechr, strrchr
頭文件:#include <string.h>
定義函數(shù):
char * index(const char *s, int c);
函數(shù)說明:index()用來找出參數(shù)s 字符串中第一個(gè)出現(xiàn)的參數(shù)c 地址,然后將該字符出現(xiàn)的地址返回。字符串結(jié)束字符(NULL)也視為字符串一部分。
返回值:如果找到指定的字符則返回該字符所在地址,否則返回0.
范例
#include <string.h>main(){ char *s = "0123456789012345678901234567890"; char *p; p = index(s, '5'); printf("%s/n", p);}執(zhí)行結(jié)果:
5.68E+25
C語言rindex()函數(shù):查找字符串并返回最后一次出現(xiàn)的位置
頭文件:#include <string.h>
定義函數(shù):
char * rindex(const char *s, int c);
函數(shù)說明:rindex()用來找出參數(shù)s 字符串中最后一個(gè)出現(xiàn)的參數(shù)c 地址,然后將該字符出現(xiàn)的地址返回。字符串結(jié)束字符(NULL)也視為字符串一部分。
返回值:如果找到指定的字符則返回該字符所在的地址,否則返回0。
范例
#include <string.h>main(){ char *s = "0123456789012345678901234567890"; char *p; p = rindex(s, '5'); printf("%s/n", p);}執(zhí)行結(jié)果:
567890
|
新聞熱點(diǎn)
疑難解答
圖片精選