找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

帖子
查看: 3466|回復(fù): 0
收起左側(cè)

strrchr函數(shù)實現(xiàn)及簡單使用

[復(fù)制鏈接]
ID:51090 發(fā)表于 2014-9-24 22:22 | 顯示全部樓層 |閱讀模式
本帖最后由 xiaojuan 于 2014-9-24 22:38 編輯

//:Vc++6.0  String strrchr函數(shù)
//功能:查找字符串s中最后一次出現(xiàn)字符c的位置
//參數(shù):str 為目標(biāo)字符串  ch 為要查找的字符
//返回值:str 為找到最后一個字符的地址
#include<stdio.h>

const char *strrchr(const char *str, int ch);

int main()
{
char str[] = "This is a sample string";
const char *pch;
pch = strrchr(str,'s');
printf("Last occurence of 's' found at %d\n",pch-str+1);
return 0;
}

const char * strrchr(const char *str, int ch)
{
if (str == NULL)
{
perror("str");
return NULL;
}

const char *temp = str;

while (*str++ != '\0');
str--;

while (str != temp) //和首地址比較
{
if (*str != (char)ch) //將ch數(shù)值強(qiáng)轉(zhuǎn)為字符型
str--;
else
return str;
}

return NULL;

}

//在vc++6.0中的運行結(jié)果為:Last occurence of 's' found at 18

回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表