C实现itoa函数的的一种方式
程序员文章站
2022-07-14 12:41:33
...
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void my_toupper(char * str)
{
while(*str != '\0')
{
if((*str >= 'a') && (*str <= 'z'))
*str += 'A' - 'a';
str++;
}
return ;
}
void my_tolower(char * str)
{
while(*str != '\0')
{
if((*str >= 'A') && (*str <= 'Z'))
*str -= 'A' - 'a';
str++;
}
return ;
}
int my_itoa(int intger, char * str)
{
int i = 0;
int j = 0;
int flag = 0;
int len = 0;
char * head = NULL;
char * tail = NULL;
flag = intger;
if(flag < 0)
{
intger = -intger;
}
do{
str[i++]=intger%10 + '0';
}while((intger/=10)>0);
if(flag < 0)
str[i++]='-';
str[i]='\0';
len = strlen(str);
head = str;
tail = str + len - 1;
while(head < tail)
{
*head ^= *tail;
*tail ^= *head;
*head ^= *tail;
head ++;
tail --;
}
return 0;
}
int main(int argc, const char * argv[])
{
char str[1024] = { 0 };
#if 0
char str[] = "dfjdflkdsfhWQJWHASDKJSD";
printf("before str: %s\n", str);
my_toupper(str);
my_tolower(str);
printf("after str: %s\n", str);
#else
if(argc < 2)
{
fprintf(stderr, "Usage %s <int_num>\n", argv[0]);
return -1;
}
my_itoa(atoi(argv[1]), str);
puts(str);
#endif
return 0;
}
测试结果
上一篇: 关于嘟嘟,引起的
下一篇: spark读取文件注意事项