C++常用库函数atoi,itoa,strcpy,strcmp的实现

本文详细介绍了如何在C++中实现常用库函数,包括将整数转换为字符串的itoa函数,字符串转换为整数的atoi函数,字符串拷贝功能的strcpy函数,以及比较两个字符串是否相等的strcmp函数的自定义实现。通过这些自定义函数,读者可以深入理解C++字符串处理的基本原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 整数转换成字符串 itoa 函数的实现

#include "stdafx.h"
#include <iostream>
using namespace std;
void itoaTest(int num,char str[] )
{
       int sign = num,i = 0,j = 0;
       char temp[11];
       if(sign<0)// 判断是否 是一个负数
       {
              num = -num;
       };
       do
       {
              temp[i] = num%10+'0';        
              num/=10;
              i++;
       }while(num>0);
       if(sign<0)
       {
              temp[i++] = '-';
       }
       temp[i] = '/0';
       i--;
       while(i>=0)
       {
              str[j] = temp[i];
              j++;
              i--;
       }
       str[j] = '/0';
}

2. // 字符串转换成整数 atoi 函数的实现

int atoiTest(char s[])
{
       int i = 0,sum = 0,sign;    // 输入的数前面可能还有空格或制表符应加判断
       while(' '==s[i]||'/t'==s[i])
       {
              i++;
       }
       sign = ('-'==s[i])?-1:1;
       if('-'==s[i]||'+'==s[i])
       {
              i++;
       }
       while(s[i]!='/0')
       {
              sum = s[i]-'0'+sum*10;
              i++;
       }    
       return sign*sum;
}

3.// 字符串拷贝函数

#include "stdafx.h"
#include <assert.h>
#include <string.h>
#include <iostream>
using namespace std;
char *srcpy(char *dest,const char *source)
{
       assert((dest!=NULL)&&(source!=NULL));
       char *address = dest;
       while(*source!='/0')
       {
              *dest++=*source++;
       }
       *dest = '/0';
       return address;
}
 

4.// 判断输入的是否是一个回文字符串

#include "stdafx.h"
#include <string.h>
#include <iostream>
using namespace std;
//方法一:借助数组
bool isPalindrome(char *input)
{
       char s[100];
       strcpy(s,input);
       int length = strlen(input);
       int begin = 0,end = length-1;
       while(begin<end)
       {
              if(s[begin]==s[end])
              {
                     begin++;
                     end--;
              }
              else
              {
                     break;
              }           
       }
       if(begin<end)
       {
              return false;
       }    
       else
       {
              return true;
       }       
}
//方法二:使用指 针
bool isPalindrome2(char *input)
{
       if(input==NULL)
              return false;
       char *begin = input;
       char *end = begin+strlen(input)-1;
       while(begin<end)
       {
              if(*begin++!=*end--)
                     return false;
       }
       return true;
}
 
int main(int argc, char* argv[])
{
       char *s ="1234554321";
       if(isPalindrome(s))
       {
              cout<<"True"<<endl;
       }
       else
       {
              cout<<"Fasle"<<endl;
       }
 
       if(isPalindrome2(s))
       {
              cout<<"True"<<endl;
       }
       else
       {
              cout<<"Fasle"<<endl;
       }
       cin.get();
 
       return 0;
}
 

5.// 不使用库函数,编写函数 int strcmp(char *source, char *dest) ,若相等返回 0 ,否则返回 -1

<pre name="code" class="cpp"><span style="font-family:SimSun;">int strcmp(char *source, char *dest)
{
       assert(source != NULL && dest != NULL);
       while(*source++==*dest++)
       {
              if(*source=='/0'&&*dest=='/0')
                     return 0;        
       }
       return -1;
}</span>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值