找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 2453|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

strspn函數(shù)實(shí)現(xiàn)及簡(jiǎn)單應(yīng)用

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:51090 發(fā)表于 2014-9-24 22:21 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
本帖最后由 xiaojuan 于 2014-9-24 22:38 編輯

//:Vc++6.0  String strspn函數(shù)
//功能:找到目標(biāo)中出現(xiàn)的字符的個(gè)數(shù)
//參數(shù):str1 字符串1  str2 字符串2
//返回值:返回查找到的個(gè)數(shù)
#include<stdio.h>

int strspn(const char *str1, const char *str2);

int main()
{
int i;
char strtext[] = "129th";
char cset[] = "1234567890";

i = strspn (strtext,cset);
printf ("The length of initial number is %d.\n",i);

return 0;
}

int strspn(const char *str1, const char *str2)
{
if (str1 == NULL || str2 == NULL)
{
perror("str1 or str2");
return -1;
}
int count = 0;
const char *temp1 = str1;
const char *temp2 = str2;

while (*temp1 != '\0')
{
temp2 = str2; //將str2 指針從新指向在字符串的首地址
while (*temp2 != '\0')
{
if (*temp2 == *temp1)
count++;
temp2++;
}
temp1++;
}
return count;
}


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

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

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

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