笔试面试记录-字符串转换成整型数等(aatoi,itoa)
2024-09-19 22:26:14
C语言中经常用到字符串与数字之间的相互转换,常见的此类库函数有atof(字符串转换成浮点数)、atoi(字符串转换成整型数)、atol(字符串转换成长整形)、itoa(整型数转换成字符串)、ltoa(长整形转换为字符串)等。
以下为自定义Myatoi()函数的实现以及测试代码。
#include <stdio.h>
int Myatoi(char*str){
if (str == NULL){
printf("Invalid Input");
return -;
}
while (*str == ' '){
str++;
}
while ((*str == (char)0xA1) && (*(str + ) == (char)0xA1)){
str += ;
}
int nSign = (*str == '-') ? - : ;//确定符号位
if (*str == '+' || *str == '-'){
str++;
}
int nResult = ;
while (*str >= ''&& *str <= ''){
nResult = nResult * + (*str - '');
str++;
}
return nResult *nSign;
} int main(){
printf("%d\n", Myatoi(""));
return ;
}
最新文章
- BZOJ 1853 【Scoi2010】 幸运数字
- 安卓手机当Mac、Win和Linux外置WIFI网卡
- 纯C++文件调用MFC类
- Andorid 编程 系统环境安装
- setInterval小问题
- 用Unity实现的依赖注入
- 如何配置apache最大的并发数
- 【HDOJ】1818 It&#39;s not a Bug, It&#39;s a Feature!
- JAVA正则表达式之贪婪、勉强和侵占
- 修改Servlet模板
- C#总结(3)
- UVa 121 - Pipe Fitters
- 查询记录rs.previous()使用
- Python获取文件夹的名字
- chrome浏览器被reimage pair 劫持怎么处理
- django celery redis 定时任务
- Linux 创建虚拟机,配置网卡,桥接,连接XShell
- RescuePro Deluxe恢复软件SanDisk
- python hashable
- golang pprof 内存分析