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.}