-->
当前位置:首页 > 题库 > 正文内容

函数题:数字字符转整数

Luz4年前 (2021-12-05)题库838
编写函数 int myatoi(const char *s);
返回字符串s中所有数字字符按从左到右的顺序组合成的整数。

### 函数接口定义:
c++
int myatoi(const char *s);


其中 s 是用户传入的字符串。 字符串s中可能含有数字字符。把s中的数字字符按从左到右的顺序组合成一个整数,返回这个整数的值。
例如,字符串 "ab3A70@9"中,含有数字字符组合成整数3709。如果字符串s中没有数字字符,则返回0。数据保证数字字符组合不超过整型表示范围。

### 裁判测试程序样例:
c++
在这里给出函数被调用进行测试的例子。例如:
#include <stdio.h>

int myatoi(const char *s);

int main(void)
{
char s1[30] = "ab6@0gap49$";
char s2[30] = "5rt1";
int x = myatoi(s1);
int y = myatoi(s2);
int z = x + y;
printf("%d + %d = %d\n", x, y, z);
return 0;
}


/* 请在这里填写答案 */


### 输入样例:

在这里给出一组输入。例如:

in



### 输出样例:

在这里给出相应的输出。例如:

out
6049 + 51 = 6100







答案:若无答案欢迎评论

评论列表

zmxncbv
zmxncbv
4年前 (2022-05-01)

int myatoi(const char *s){
int a=0,i=0;
while(*s!='\0'){
if(*s˃='0'&&*s˂='9')
a=a*10+*s-'0';
s++;
}
return a;
}

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。