找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2660|回复: 0
收起左侧

strspn函数实现及简单应用

[复制链接]
ID:51090 发表于 2014-9-24 22:21 | 显示全部楼层 |阅读模式
本帖最后由 xiaojuan 于 2014-9-24 22:38 编辑

//:Vc++6.0  String strspn函数
//功能:找到目标中出现的字符的个数
//参数:str1 字符串1  str2 字符串2
//返回值:返回查找到的个数
#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;
}


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表