1.#include "stdafx.h"2.using namespace std;
3.//--------字符替代问题4.char* Translate(char* pSrc, const char cFindChar, const char* pReplaceStr, char* pTag)
5. {
6. char* p = NULL ;
7. int lLen = (int)strlen (pReplaceStr) ;
8.
9. for (p = pSrc; *p!='\0'; p++)
10. {
11. if (cFindChar == *p)
12. {
13. strcpy(pTag, pReplaceStr) ;
14. pTag += lLen ;
15. }
16. else
17. {
18. *pTag++ = *p;
19. }
20. }
21. return pTag;
22. }
23.24.//--------字符串转换为整数25.int myAtoi(const char* s){
26. int result = 0;
27. int flag = 1;
28. int i = 0;
29. while(isspace(s[i]))
30. i++;
31. if(s[i] == '-'){
32. flag = -1;
33. i++;
34. }
35. if(s[i] == '+')
36. i++;
37. while(s[i] != '\0'){
38. if((s[i] > '9') || (s[i] < '0'))
39. break;
40. int j = s[i] - '0';
41. result = 10 * result + j;
42. i++;
43. }
44. result = result * flag;
45. return result;
46.}
47.48.//--------整数转换为字符串49. void itoa (int n,char s[])
50. {
51. int i,j,sign;
52.53. if((sign=n)<0)//记录符号54. n=-n;//使n成为正数55. i=0;
56. do{
57. s[i++]=n%10+'0';//取下一个数字58. }while ((n/=10)>0);//删除该数字59.60. if(sign<0)
61. s[i++]='-';
62. s[i]='\0';
63.64. for(j=i;j>=0;j--)//生成的数字是逆序的,所以要逆序输出65. printf("%c",s[j]);
66. printf("\n");
67.68. }
69.int _tmain(int argc, _TCHAR* argv[])
70.{
71. //--------Translate----------72. char szSrc[] = "Hello Word!" ;
73. char szTag[256]= {0};
74. char szRep[] ="(***)";
75. char result[256] = {0};
76. Translate(szSrc,'o',szRep,szTag);
77. for(unsigned int i=0;i<strlen(szTag);i++) result[i] = szTag[i];
78. printf("%s\n",szTag);
79. printf("%s\n",result);
80. //-----------------------81. int i = atoi(" -321" );
82. printf("%d\n",i);
83. char ch[256]={0};
84. itoa(-23456, ch);
85.
86. return 0;
87.}
88.
文章出处:飞诺网( www.diybl.com):http://www.diybl.com/course/3_program/c/c_js/2008828/138143.html
3.//--------字符替代问题4.char* Translate(char* pSrc, const char cFindChar, const char* pReplaceStr, char* pTag)
5. {
6. char* p = NULL ;
7. int lLen = (int)strlen (pReplaceStr) ;
8.
9. for (p = pSrc; *p!='\0'; p++)
10. {
11. if (cFindChar == *p)
12. {
13. strcpy(pTag, pReplaceStr) ;
14. pTag += lLen ;
15. }
16. else
17. {
18. *pTag++ = *p;
19. }
20. }
21. return pTag;
22. }
23.24.//--------字符串转换为整数25.int myAtoi(const char* s){
26. int result = 0;
27. int flag = 1;
28. int i = 0;
29. while(isspace(s[i]))
30. i++;
31. if(s[i] == '-'){
32. flag = -1;
33. i++;
34. }
35. if(s[i] == '+')
36. i++;
37. while(s[i] != '\0'){
38. if((s[i] > '9') || (s[i] < '0'))
39. break;
40. int j = s[i] - '0';
41. result = 10 * result + j;
42. i++;
43. }
44. result = result * flag;
45. return result;
46.}
47.48.//--------整数转换为字符串49. void itoa (int n,char s[])
50. {
51. int i,j,sign;
52.53. if((sign=n)<0)//记录符号54. n=-n;//使n成为正数55. i=0;
56. do{
57. s[i++]=n%10+'0';//取下一个数字58. }while ((n/=10)>0);//删除该数字59.60. if(sign<0)
61. s[i++]='-';
62. s[i]='\0';
63.64. for(j=i;j>=0;j--)//生成的数字是逆序的,所以要逆序输出65. printf("%c",s[j]);
66. printf("\n");
67.68. }
69.int _tmain(int argc, _TCHAR* argv[])
70.{
71. //--------Translate----------72. char szSrc[] = "Hello Word!" ;
73. char szTag[256]= {0};
74. char szRep[] ="(***)";
75. char result[256] = {0};
76. Translate(szSrc,'o',szRep,szTag);
77. for(unsigned int i=0;i<strlen(szTag);i++) result[i] = szTag[i];
78. printf("%s\n",szTag);
79. printf("%s\n",result);
80. //-----------------------81. int i = atoi(" -321" );
82. printf("%d\n",i);
83. char ch[256]={0};
84. itoa(-23456, ch);
85.
86. return 0;
87.}
88.
文章出处:飞诺网( www.diybl.com):http://www.diybl.com/course/3_program/c/c_js/2008828/138143.html