45fan.com - 路饭网

搜索: 您的位置主页 > 网络频道 > 阅读资讯:使用两个字符串操作函数的方法

使用两个字符串操作函数的方法

2016-09-02 09:26:55 来源:www.45fan.com 【

使用两个字符串操作函数的方法

/*!brief 复制字符串的后一部分

编写函数strmcpy(s,t,m),将字符串从第m个字符开始的全部字符复制到字串s中去。n

难度系数:简单

param [in] source 源字符串.只读

param [in] dest 目的字符串

param [in] nStart 从第nStart个字符开始复制

bug

-# 未考虑strlen(source) < nStart的情况.(出错)

-# 未考虑dest[]空间不足的情况.(出错)

-# 未考虑source不以'/0'结尾的情况.(出错)

.

pre source is zero ending && strlen(source) > nStart && sizeof(dest) > sizeof(source)

post dest is zero ending && strcmp(dest, source + nStart) == 0

*/

void LeftSubStr(char *source, char * dest, int nStart)

{

char *p = source + nStart;

char *q = dest;

while (*q++ = *p++);

}

//4.编写函数,输入一个字符串,内有数字和非数字字符,将其连续的数字作为一个整数,依次存放到数组A中,并统计有多少个整数。

//不使用库的话:

int isdigit(int c)

{

return !(c < '0' || c >'9');

}

int atoi(char* str)

{

char *p = str;

int sum = *p - '0';

while( isdigit(*++p))

{

sum = sum * 10 + (*p - '0');

}

return sum;

}

int findDigital(char* str, int buf[], int nLen)

{

char *p = str;

int i;

for (i = 0; i< nLen && *p; i++)

{

while(*p && !isdigit(*++p));

buf[i] = atoi(p);

while(isdigit(*++p));

}

return i;

}

#include <stdio.h>

int main ()

{

int a[10];

int len = findDigital("-11,2", a, 10);

printf("%d 个, %d, %d", len, a[0], a[1]);

return 0;

}

本文地址:http://www.45fan.com/a/question/71153.html
Tags: 两个 字符串 操作
编辑:路饭网
关于我们 | 联系我们 | 友情链接 | 网站地图 | Sitemap | App | 返回顶部