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

相关文章

SpringBoot实现微信小程序支付功能

《SpringBoot实现微信小程序支付功能》小程序支付功能已成为众多应用的核心需求之一,本文主要介绍了SpringBoot实现微信小程序支付功能,文中通过示例代码介绍的非常详细,对大家的学习或者工作... 目录一、引言二、准备工作(一)微信支付商户平台配置(二)Spring Boot项目搭建(三)配置文件

如何高效移除C++关联容器中的元素

《如何高效移除C++关联容器中的元素》关联容器和顺序容器有着很大不同,关联容器中的元素是按照关键字来保存和访问的,而顺序容器中的元素是按它们在容器中的位置来顺序保存和访问的,本文介绍了如何高效移除C+... 目录一、简介二、移除给定位置的元素三、移除与特定键值等价的元素四、移除满足特android定条件的元

基于Python实现高效PPT转图片工具

《基于Python实现高效PPT转图片工具》在日常工作中,PPT是我们常用的演示工具,但有时候我们需要将PPT的内容提取为图片格式以便于展示或保存,所以本文将用Python实现PPT转PNG工具,希望... 目录1. 概述2. 功能使用2.1 安装依赖2.2 使用步骤2.3 代码实现2.4 GUI界面3.效

MySQL更新某个字段拼接固定字符串的实现

《MySQL更新某个字段拼接固定字符串的实现》在MySQL中,我们经常需要对数据库中的某个字段进行更新操作,本文就来介绍一下MySQL更新某个字段拼接固定字符串的实现,感兴趣的可以了解一下... 目录1. 查看字段当前值2. 更新字段拼接固定字符串3. 验证更新结果mysql更新某个字段拼接固定字符串 -

Python获取C++中返回的char*字段的两种思路

《Python获取C++中返回的char*字段的两种思路》有时候需要获取C++函数中返回来的不定长的char*字符串,本文小编为大家找到了两种解决问题的思路,感兴趣的小伙伴可以跟随小编一起学习一下... 有时候需要获取C++函数中返回来的不定长的char*字符串,目前我找到两种解决问题的思路,具体实现如下:

java实现延迟/超时/定时问题

《java实现延迟/超时/定时问题》:本文主要介绍java实现延迟/超时/定时问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java实现延迟/超时/定时java 每间隔5秒执行一次,一共执行5次然后结束scheduleAtFixedRate 和 schedu

Java Optional避免空指针异常的实现

《JavaOptional避免空指针异常的实现》空指针异常一直是困扰开发者的常见问题之一,本文主要介绍了JavaOptional避免空指针异常的实现,帮助开发者编写更健壮、可读性更高的代码,减少因... 目录一、Optional 概述二、Optional 的创建三、Optional 的常用方法四、Optio

C++ Sort函数使用场景分析

《C++Sort函数使用场景分析》sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变,如果某些场景需要保持相同元素间的相对顺序,可使... 目录C++ Sort函数详解一、sort函数调用的两种方式二、sort函数使用场景三、sort函数排序

在Android平台上实现消息推送功能

《在Android平台上实现消息推送功能》随着移动互联网应用的飞速发展,消息推送已成为移动应用中不可或缺的功能,在Android平台上,实现消息推送涉及到服务端的消息发送、客户端的消息接收、通知渠道(... 目录一、项目概述二、相关知识介绍2.1 消息推送的基本原理2.2 Firebase Cloud Me

Spring Boot项目中结合MyBatis实现MySQL的自动主从切换功能

《SpringBoot项目中结合MyBatis实现MySQL的自动主从切换功能》:本文主要介绍SpringBoot项目中结合MyBatis实现MySQL的自动主从切换功能,本文分步骤给大家介绍的... 目录原理解析1. mysql主从复制(Master-Slave Replication)2. 读写分离3.