Realization of common library function atoi,itoa,strcpy,strcmp in C + + __jquery

Source: Internet
Author: User
Tags assert strcmp

Realization of C + + Common library function atoi,itoa,strcpy,strcmp

View Plaincopy to Clipboardprint?

Implementation of 1 1.//integer conversion into string itoa function

2 #include "stdafx.h"

3 #include <iostream>

4 using namespace Std;

5 void itoatest (int num,char str[])

6 {

7 int sign = Num,i = 0,j = 0;

8 Char temp[11];

9 if (sign<0)//To determine whether it is a negative number

10 {

num =-num;

12};

Do

14 {

Temp[i] = num%10+ ' 0 ';

num/=10;

i++;

}while (num>0);

if (sign<0)

20 {

temp[i++] = '-';

22}

Temp[i] = ' the ';

i--;

while (i>=0)

26 {

STR[J] = Temp[i];

j + +;

i--;

30}

STR[J] = ' the ';

32}

33 2. The implementation of string conversion into integer atoi function

int Atoitest (char s[])

35 {

int i = 0,sum = 0,sign; The number you entered may be preceded by a space or a tab should be judged

Notoginseng while (' ==s[i]| | ' \ t ' ==s[i])

38 {

i++;

40}

Sign = ('-' ==s[i])? -1:1;

if ('-' ==s[i]| | ') + ' ==s[i])

43 {

i++;

45}

while (s[i]!= ' ")

47 {

sum = s[i]-' 0 ' +sum*10;

i++;

50}

Sign*sum;

52}

53

54

55 3.//String Copy function

#include "stdafx.h"

#include <assert.h>

#include <string.h>

#include <iostream>

using namespace Std;

*srcpy Char (char *dest,const char *source)

62 {

An ASSERT ((dest!=null) && (source!=null));

The char *address = dest;

while (*source!= ' ")

66 {

*dest++=*source++;

68}

*dest = ' the ";

return to address;

71}

72

73 4.//to determine if the input is a palindrome string

#include "stdafx.h"

#include <string.h>

#include <iostream>

The using namespace Std;

78//Method one: With the help of array

Ispalindrome-BOOL (char *input)

80 {

Bayi Char s[100];

strcpy (S,input);

Length of int = strlen (input);

The int begin = 0,end = Length-1;

while (Begin<end)

86 {

if (S[begin]==s[end])

88 {

begin++;

end--;

91}

He else

93 {

The break;

95}

96}

if (begin<end)

98 {

return false;

100}

Or else

102 {

The return true;

104}

105}

106//Method two: Use pointers

isPalindrome2 bool (char *input)

108 {

109 if (input==null)

The return false;

+ Char *begin = input;

112 Char *end = Begin+strlen (input)-1;

113 while (begin<end)

114 {

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

if (isPalindrome2 (s))

134 {

135 cout<< "True" <<endl;

136}

137 Else

138 {

139 cout<< "Fasle" <<endl;

140}

Cin.get ();

142

143 return 0;

144}

145

146

147 5.//does not use the library function, writes the function int strcmp (char *source, char *dest), if equal returns 0, otherwise returns-1

148 int strcmp (char *source, char *dest)

149 {

An assert (source!= null && dest!= null);

151 while (*source++==*dest++)

152 {

153 if (*source== ' &&*dest== ')

154 return 0;

155}

156 return-1;

157}


View Plaincopy to Clipboardprint

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.