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

相关文章

HarmonyOS学习(七)——UI(五)常用布局总结

自适应布局 1.1、线性布局(LinearLayout) 通过线性容器Row和Column实现线性布局。Column容器内的子组件按照垂直方向排列,Row组件中的子组件按照水平方向排列。 属性说明space通过space参数设置主轴上子组件的间距,达到各子组件在排列上的等间距效果alignItems设置子组件在交叉轴上的对齐方式,且在各类尺寸屏幕上表现一致,其中交叉轴为垂直时,取值为Vert

JS常用组件收集

收集了一些平时遇到的前端比较优秀的组件,方便以后开发的时候查找!!! 函数工具: Lodash 页面固定: stickUp、jQuery.Pin 轮播: unslider、swiper 开关: switch 复选框: icheck 气泡: grumble 隐藏元素: Headroom

hdu1043(八数码问题,广搜 + hash(实现状态压缩) )

利用康拓展开将一个排列映射成一个自然数,然后就变成了普通的广搜题。 #include<iostream>#include<algorithm>#include<string>#include<stack>#include<queue>#include<map>#include<stdio.h>#include<stdlib.h>#include<ctype.h>#inclu

【C++ Primer Plus习题】13.4

大家好,这里是国中之林! ❥前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站。有兴趣的可以点点进去看看← 问题: 解答: main.cpp #include <iostream>#include "port.h"int main() {Port p1;Port p2("Abc", "Bcc", 30);std::cout <<

C++包装器

包装器 在 C++ 中,“包装器”通常指的是一种设计模式或编程技巧,用于封装其他代码或对象,使其更易于使用、管理或扩展。包装器的概念在编程中非常普遍,可以用于函数、类、库等多个方面。下面是几个常见的 “包装器” 类型: 1. 函数包装器 函数包装器用于封装一个或多个函数,使其接口更统一或更便于调用。例如,std::function 是一个通用的函数包装器,它可以存储任意可调用对象(函数、函数

C++11第三弹:lambda表达式 | 新的类功能 | 模板的可变参数

🌈个人主页: 南桥几晴秋 🌈C++专栏: 南桥谈C++ 🌈C语言专栏: C语言学习系列 🌈Linux学习专栏: 南桥谈Linux 🌈数据结构学习专栏: 数据结构杂谈 🌈数据库学习专栏: 南桥谈MySQL 🌈Qt学习专栏: 南桥谈Qt 🌈菜鸡代码练习: 练习随想记录 🌈git学习: 南桥谈Git 🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈�

【C++】_list常用方法解析及模拟实现

相信自己的力量,只要对自己始终保持信心,尽自己最大努力去完成任何事,就算事情最终结果是失败了,努力了也不留遗憾。💓💓💓 目录   ✨说在前面 🍋知识点一:什么是list? •🌰1.list的定义 •🌰2.list的基本特性 •🌰3.常用接口介绍 🍋知识点二:list常用接口 •🌰1.默认成员函数 🔥构造函数(⭐) 🔥析构函数 •🌰2.list对象

【Prometheus】PromQL向量匹配实现不同标签的向量数据进行运算

✨✨ 欢迎大家来到景天科技苑✨✨ 🎈🎈 养成好习惯,先赞后看哦~🎈🎈 🏆 作者简介:景天科技苑 🏆《头衔》:大厂架构师,华为云开发者社区专家博主,阿里云开发者社区专家博主,CSDN全栈领域优质创作者,掘金优秀博主,51CTO博客专家等。 🏆《博客》:Python全栈,前后端开发,小程序开发,人工智能,js逆向,App逆向,网络系统安全,数据分析,Django,fastapi

常用的jdk下载地址

jdk下载地址 安装方式可以看之前的博客: mac安装jdk oracle 版本:https://www.oracle.com/java/technologies/downloads/ Eclipse Temurin版本:https://adoptium.net/zh-CN/temurin/releases/ 阿里版本: github:https://github.com/

让树莓派智能语音助手实现定时提醒功能

最初的时候是想直接在rasa 的chatbot上实现,因为rasa本身是带有remindschedule模块的。不过经过一番折腾后,忽然发现,chatbot上实现的定时,语音助手不一定会有响应。因为,我目前语音助手的代码设置了长时间无应答会结束对话,这样一来,chatbot定时提醒的触发就不会被语音助手获悉。那怎么让语音助手也具有定时提醒功能呢? 我最后选择的方法是用threading.Time