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

2023-10-25 12:32

本文主要是介绍C++常用库函数atoi,itoa,strcpy,strcmp的实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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


view plaincopy to clipboardprint?
01.1.//整数转换成字符串itoa函数的实现 
02.#include "stdafx.h" 
03.#include <iostream>  
04.using namespace std; 
05.void itoaTest(int num,char str[] ) 
06.{ 
07.       int sign = num,i = 0,j = 0; 
08.       char temp[11]; 
09.       if(sign<0)//判断是否是一个负数  
10.       { 
11.              num = -num; 
12.       }; 
13.       do 
14.       { 
15.              temp[i] = num%10+'0';         
16.              num/=10; 
17.              i++; 
18.       }while(num>0); 
19.       if(sign<0) 
20.       { 
21.              temp[i++] = '-'; 
22.       } 
23.       temp[i] = '/0'; 
24.       i--; 
25.       while(i>=0) 
26.       { 
27.              str[j] = temp[i]; 
28.              j++; 
29.              i--; 
30.       } 
31.       str[j] = '/0'; 
32.} 
33.2. //字符串转换成整数atoi函数的实现  
34.int atoiTest(char s[]) 
35.{ 
36.       int i = 0,sum = 0,sign;    //输入的数前面可能还有空格或制表符应加判断  
37.       while(' '==s[i]||'/t'==s[i]) 
38.       { 
39.              i++; 
40.       } 
41.       sign = ('-'==s[i])?-1:1; 
42.       if('-'==s[i]||'+'==s[i]) 
43.       { 
44.              i++; 
45.       } 
46.       while(s[i]!='/0') 
47.       { 
48.              sum = s[i]-'0'+sum*10; 
49.              i++; 
50.       }     
51.       return sign*sum; 
52.} 
53.  
54.  
55.3.//字符串拷贝函数 
56.#include "stdafx.h" 
57.#include <assert.h> 
58.#include <string.h> 
59.#include <iostream>  
60.using namespace std; 
61.char *srcpy(char *dest,const char *source) 
62.{ 
63.       assert((dest!=NULL)&&(source!=NULL)); 
64.       char *address = dest; 
65.       while(*source!='/0') 
66.       { 
67.              *dest++=*source++; 
68.       } 
69.       *dest = '/0'; 
70.       return address; 
71.} 
72.  
73.4.//判断输入的是否是一个回文字符串 
74.#include "stdafx.h" 
75.#include <string.h> 
76.#include <iostream>  
77.using namespace std; 
78.//方法一:借助数组  
79.bool isPalindrome(char *input) 
80.{ 
81.       char s[100]; 
82.       strcpy(s,input); 
83.       int length = strlen(input); 
84.       int begin = 0,end = length-1; 
85.       while(begin<end) 
86.       { 
87.              if(s[begin]==s[end]) 
88.              { 
89.                     begin++; 
90.                     end--; 
91.              } 
92.              else 
93.              { 
94.                     break; 
95.              }            
96.       } 
97.       if(begin<end) 
98.       { 
99.              return false; 
100.       }     
101.       else 
102.       { 
103.              return true; 
104.       }       
105.} 
106.//方法二:使用指针  
107.bool isPalindrome2(char *input) 
108.{ 
109.       if(input==NULL) 
110.              return false; 
111.       char *begin = input; 
112.       char *end = begin+strlen(input)-1; 
113.       while(begin<end) 
114.       { 
115.              if(*begin++!=*end--) 
116.                     return false; 
117.       } 
118.       return true; 
119.} 
120.  
121.int main(int argc, char* argv[]) 
122.{ 
123.       char *s ="1234554321"; 
124.       if(isPalindrome(s)) 
125.       { 
126.              cout<<"True"<<endl; 
127.       } 
128.       else 
129.       { 
130.              cout<<"Fasle"<<endl; 
131.       } 
132.  
133.       if(isPalindrome2(s)) 
134.       { 
135.              cout<<"True"<<endl; 
136.       } 
137.       else 
138.       { 
139.              cout<<"Fasle"<<endl; 
140.       } 
141.       cin.get(); 
142.  
143.       return 0; 
144.} 
145.  
146.  
147.5.//不使用库函数,编写函数int strcmp(char *source, char *dest),若相等返回0,否则返回-1  
148.int strcmp(char *source, char *dest) 
149.{ 
150.       assert(source != NULL && dest != NULL); 
151.       while(*source++==*dest++) 
152.       { 
153.              if(*source=='/0'&&*dest=='/0') 
154.                     return 0;         
155.       } 
156.       return -1; 
157.} 
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
int strcmp(char *source, char *dest)
{
       assert(source != NULL && dest != NULL);
       while(*source++==*dest++)
       {
              if(*source=='/0'&&*dest=='/0')
                     return 0;       
       }
       return -1;
}

view plaincopy to clipboardprint?
01.#include <stdio.h>  
02.void strcat(char *string1, char *string2){ 
03.    while(*string1 != '/0') 
04.        string1++; 
05.    while(*string2) 
06.    { 
07.        *string1++ = *string2++; 
08.    } 
09.    *string1++ = '/0'; 
10.} 
11.int strlen(char *string1){ 
12.    int count = 0; 
13.    while(*string1++ != '/0') 
14.        count++; 
15.    return count; 
16.} 
17.int main(void) 
18.{ 
19.    char name[100]="wangfeng"; 
20.    char *mesg = " is a student!"; 
21.    strlen(name); 
22.    puts(name); 
23.    return 0; 
24.} 
25.#include <stdlib.h>  
26./*
27.   这个函数调用的是库函数中的
28.   strtol()函数,关于这个函数的
29.   源代码后面将会给出。
30.*/ 
31.int my_atoi(char *str) 
32.{ 
33.   return (int) strtol(str, NULL, 10); 
34.} 
35./*
36.   下面的两个函数没有调用strtol()函数,
37.   而是直接给出了该函数的实现。
38.*/ 
39.int my_atoi01(const char *str) 
40.{ 
41.   long int v=0; 
42.   int sign = 0; 
43.  
44.   while ( *str == ' ')  str++; 
45.  
46.   if(*str == '-'||*str == '+') 
47.      sign = *str++; 
48.  
49.   while (isdigit(*str)) 
50.   { 
51.      v = v*10 + *str - '0'; 
52.      str++; 
53.   } 
54.   return sign == '-' ? -v:v; 
55.} 
56.int my_atoi02(char *str) 
57.{ 
58.   int sign; 
59.   int n; 
60.   unsigned char *p; 
61.  
62.   p=str; 
63.   while (isspace(*p) ) p++; 
64.  
65.   sign = (*p == '-' ) ? -1 : 1; 
66.  
67.   if (*p=='+' || *p=='-' ) p++; 
68.  
69.   for (n=0; isdigit(*p); p++) 
70.      n = 10*n + (*p - '0'); 
71.  
72.   return sign*n; 
73.} 
74.  
75.int main() 
76.{ 
77.   char * str = "2147483647"; 
78.   printf("%d/n",my_atoi(str)); 
79.   
80.   str = "-2147483648"; 
81.   printf("%d/n",my_atoi(str)); 
82.   
83.   str = "2147483647"; 
84.   printf("%d/n",my_atoi01(str)); 
85.   
86.   str = "-2147483648"; 
87.   printf("%d/n",my_atoi01(str)); 
88.   
89.   str = "2147483647"; 
90.   printf("%d/n",my_atoi02(str)); 
91.   
92.   str = "-2147483648"; 
93.   printf("%d/n",my_atoi02(str)); 
94.   
95.   system("pause"); 
96.   return 0; 
97.} 

 

这篇关于C++常用库函数atoi,itoa,strcpy,strcmp的实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/282609

相关文章

Spring Boot中WebSocket常用使用方法详解

《SpringBoot中WebSocket常用使用方法详解》本文从WebSocket的基础概念出发,详细介绍了SpringBoot集成WebSocket的步骤,并重点讲解了常用的使用方法,包括简单消... 目录一、WebSocket基础概念1.1 什么是WebSocket1.2 WebSocket与HTTP

Python使用python-can实现合并BLF文件

《Python使用python-can实现合并BLF文件》python-can库是Python生态中专注于CAN总线通信与数据处理的强大工具,本文将使用python-can为BLF文件合并提供高效灵活... 目录一、python-can 库:CAN 数据处理的利器二、BLF 文件合并核心代码解析1. 基础合

Python使用OpenCV实现获取视频时长的小工具

《Python使用OpenCV实现获取视频时长的小工具》在处理视频数据时,获取视频的时长是一项常见且基础的需求,本文将详细介绍如何使用Python和OpenCV获取视频时长,并对每一行代码进行深入解析... 目录一、代码实现二、代码解析1. 导入 OpenCV 库2. 定义获取视频时长的函数3. 打开视频文

golang版本升级如何实现

《golang版本升级如何实现》:本文主要介绍golang版本升级如何实现问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录golanwww.chinasem.cng版本升级linux上golang版本升级删除golang旧版本安装golang最新版本总结gola

SpringBoot中SM2公钥加密、私钥解密的实现示例详解

《SpringBoot中SM2公钥加密、私钥解密的实现示例详解》本文介绍了如何在SpringBoot项目中实现SM2公钥加密和私钥解密的功能,通过使用Hutool库和BouncyCastle依赖,简化... 目录一、前言1、加密信息(示例)2、加密结果(示例)二、实现代码1、yml文件配置2、创建SM2工具

Mysql实现范围分区表(新增、删除、重组、查看)

《Mysql实现范围分区表(新增、删除、重组、查看)》MySQL分区表的四种类型(范围、哈希、列表、键值),主要介绍了范围分区的创建、查询、添加、删除及重组织操作,具有一定的参考价值,感兴趣的可以了解... 目录一、mysql分区表分类二、范围分区(Range Partitioning1、新建分区表:2、分

MySQL 定时新增分区的实现示例

《MySQL定时新增分区的实现示例》本文主要介绍了通过存储过程和定时任务实现MySQL分区的自动创建,解决大数据量下手动维护的繁琐问题,具有一定的参考价值,感兴趣的可以了解一下... mysql创建好分区之后,有时候会需要自动创建分区。比如,一些表数据量非常大,有些数据是热点数据,按照日期分区MululbU

golang中reflect包的常用方法

《golang中reflect包的常用方法》Go反射reflect包提供类型和值方法,用于获取类型信息、访问字段、调用方法等,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值... 目录reflect包方法总结类型 (Type) 方法值 (Value) 方法reflect包方法总结

MySQL中查找重复值的实现

《MySQL中查找重复值的实现》查找重复值是一项常见需求,比如在数据清理、数据分析、数据质量检查等场景下,我们常常需要找出表中某列或多列的重复值,具有一定的参考价值,感兴趣的可以了解一下... 目录技术背景实现步骤方法一:使用GROUP BY和HAVING子句方法二:仅返回重复值方法三:返回完整记录方法四:

IDEA中新建/切换Git分支的实现步骤

《IDEA中新建/切换Git分支的实现步骤》本文主要介绍了IDEA中新建/切换Git分支的实现步骤,通过菜单创建新分支并选择是否切换,创建后在Git详情或右键Checkout中切换分支,感兴趣的可以了... 前提:项目已被Git托管1、点击上方栏Git->NewBrancjsh...2、输入新的分支的