C语言—atoi()、itoa()函数以及my_atoi()、my_itoa()函数详解

文章详细介绍了C语言中将字符串转换为整数的atoi函数以及将整数转换为字符串的itoa函数的使用方法。同时,提供了自定义实现my_atoi和my_itoa的代码示例,包括处理正负号、空格和非数字字符的情况。
摘要由CSDN通过智能技术生成

1 atoi()函数

atoi函数是将字符串转换成整数。头文件<stdlib.h>

注意:
1、数字字符前有空格存在则跳过。
2、数字字符前有+、-号作为整数的正负号处理。
3、数字字符前有其他字符则返回0。

atoi()函数的使用:

#define _CRT_SECURE_NO_WARNINGS
#include<stdlib.h>
#include <stdio.h>

int main() {
	//atoi 字符串转整形   处理空格、数字、正负开头;其余开头返回0
	//int num = atoi("123"); //123
	//int num = atoi("  a123"); //0
	//int num = atoi("  -123 ");//-123
    //int num = atoi("  +123 "); //123
	int num = atoi("  12abc3 ");//12
    printf("%d\n", num);
}

2 itoa()函数

itoa()函数是将整数类型转为字符串。头文件<stdlib.h>

itoa()函数有三个参数:
第一个参数是要转换的数字
第二个参数是要写入转换结果的目标字符串
第三个参数是转移数字时所用的基数

itoa()函数的使用:

#define _CRT_SECURE_NO_WARNINGS
#include<stdlib.h>
#include <stdio.h>

int main() {
     //itoa 整形转字符串
	char buff[128] = { 0 };
	//_itoa(100, buff, 10);//100  100以十进制存储在buff中
	//_itoa(100, buff, 8);//144   100以八进制存储在buff中
	_itoa(100, buff, 16);//64      100以十六进制存储在buff中
	printf("%s\n", buff);
}

3 my_atoi()函数的实现

有改进空间!!!

#define _CRT_SECURE_NO_WARNINGS
#include <math.h>
#include <cassert>
#include <stdio.h>
#include <stdlib.h>//atoi  itoa
#include<string.h>
#include<ctype.h>//isdigit()


//统计连续数字字符个数
int getcount(const char* str) {
    int count = 0;
    int len = strlen(str);
    for (int i = 0; i < len-1; i++) {//不判断如abc123类,核心部分判断
        if (str[i] == ' '||str[i]=='-'||str[i]=='+') continue;
        if (isdigit(str[i])&&isdigit(str[i+1])) {
            count++;
        }
        else {
            break;
        }
    }
    return count+1;
}

int my_atoi(const char* str) {
    assert(str != NULL);
    while (*str == ' ') {  //排除空格
        str++;
    }
    if (*str != '+' && *str != '-' && !isdigit(*str)) {   //空格之后为非符号且非数字字符
        return 0;
    }
    int bit = getcount(str);  //连续数字字符出现的个数
    int result = 0 , flag = 1;
    while (*str != '\0') {
        if (*str == '-') {
            flag = -1;
            str++;
            if (*str == '-' || *str == '+')
                return 0;
            
        }
        else if (*str == '+') {
            flag = 1;
            str++;
            if (*str == '-' || *str == '+') 
                return 0;
            
        }
        else {
            result += (*str - '0') * (int)pow(10, bit - 1);
            str++;
            bit--;

        }
                   
    }
    return result*flag;
}

int main() {
    //int value = my_atoi("  -12sd3"); -12
    //int value = my_atoi(" -123abc");-123
    //int value = my_atoi("  +123abc3"); 123
    int value = my_atoi("  abc123abc"); //0   
 
    printf("%d\n", value);

    return 0;
}

4 my_itoa()函数的实现

思想较my_atoi()难。

#define _CRT_SECURE_NO_WARNINGS
#include <math.h>
#include <cassert>
#include <stdio.h>
#include <stdlib.h>//atoi  itoa
#include<string.h>

char* my_itoa(int value, char* buff, int radix) {
    assert(buff != NULL && radix >= 2);
    //辗转相除法
    //查表法
    char alpha[] = "0123456789abcdefghijklmnopqrstuvwxyz";
    int flag = 0;
    if (value < 0 && radix == 10) {
        buff[0] = '-';
        value *= -1;
        flag = 1;
    }

    unsigned int val = (unsigned int)value;
    int i = flag;
    while (val != 0) {
        buff[i++] = alpha[val % radix];
        val /= radix;
    }
    buff[i] = '\0';
    for (int j = 0; j < (i - 1)/2; j++) {
        char temp = buff[j+flag];
        buff[j+flag] = buff[i - 1 - j];
        buff[i - 1 - j] = temp;
    }
    return buff;
}

int main() {
    char buff[128] = { 0 };
    //my_itoa(100, buff, 8);//144
    //my_itoa(10, buff, 16);//a
    //my_itoa(10, buff, 2);//1010
    //my_itoa(-1, buff, 2);//11111111111111111111111111111111
    //my_itoa(-1, buff, 16);//ffffffff
    //my_itoa(-1, buff, 8);//37777777777   32个1  三位划分 开头补0  011--3 111-7  ……
    //my_itoa(-1, buff, 10);//-1
    my_itoa(-1234, buff, 10);//-1234
    printf("%s\n", buff);

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值