atoi、itoa,strcpy,strcmp,memcpy等实现

1、memcpy、memmove、memset源码
2、strcpy、strcat等源码
3、atoi和itoa源码:

整数字符串的转化

1、直接采用现有函数

(1)直接采用itoa实现整数到字符串的转换

  函数形式为: char *itoa(int value, char *string, int radix);

  该函数包含在头文件stdlib.h中。int value 被转换的整数,char *string 转换后储存的字符数组,int radix 转换进制数,如2,8,10,16 进制等。

  具体实现为:

View Code
1 #include<stdlib.h>
2 #include<stdio.h>
3 void main()
4 {
5     int a=123;
6     char string[10];
7     itoa(a,string,10);
8     printf("整数:%d\n字符串:%s",a,string);
9 }

(2)直接采用atoi实现字符串到整数的转换

  函数形式为: int atoi(const char *nptr);

    函数说明: 参数nptr字符串,如果第一个非空格字符不存在或者不是数字也不是正负号则返回零,否则开始做类型转换,之后检测到非数字(包括结束符 \0) 字符时停止转换,返回整型数。
  具体实现为:
View Code
1 #include<stdlib.h>
2 #include<stdio.h>
3 void main()
4 {
5     int a;
6     char string[10]="123";
7     a=atoi(string);
8     printf("转换后的整数:%d",a);
9 }

2、不采用现有的itoa和atoi函数

(1)整数到字符串的转换

  具体实现为:

View Code
 1 #include<stdlib.h>
 2 #include<stdio.h>
 3 void main()
 4 {
 5     int i,j=0,a=123456;
 6     char temp[10],string[10];
 7     while(a)
 8     {
 9         i=a%10+'0';
10         temp[j++]=i;
11         a=a/10;
12     }
13     i=0;
14     j--;
15     while(j>=0)
16         string[i++]=temp[j--];
17     string[i]='\0';
18     printf("转换成的字符串为:%s",string);
19 }

(2)字符串到整数的转换

  具体实现为:

View Code
 1 #include<stdlib.h>
 2 #include<stdio.h>
 3 #include<string>
 4 void main()
 5 {
 6     char temp[10],string[10]="123456";
 7     int i=0,j,a=0;
 8     strcpy(temp,string);
 9     while(temp[i])
10     {
11         a=a*10+(temp[i]-'0');
12         i++;
13     }
14     printf("转换为整数:%d",a);
15 }


14、2 字符数组和strcpy

(1)面试题1

  strcpy字符串拷贝函数的实现

  strcpy的原型为:char*strcpy(char *strDest,const char *strSrc)

  具体实现为:

View Code
 1 #include<stdlib.h>
 2 #include<stdio.h>
 3 char *strcpy1(char *strDest,char *strSrc)
 4 {
 5     char *temp=strDest;
 6     while((*strDest=*strSrc)!='\0')
 7     {
 8         strDest++;
 9         strSrc++;
10     }
11     return temp;
12 }
13 
14 void main()
15 {
16     char str[]="123iamhappy!";
17     char strDest[50];
18     strcpy1(strDest,str);
19     printf("最终的结果为:%s\n",strDest);    
20 }

(2)面试题3

  编写一个函数,把一个char组成的字符串循环右移n位

  具体实现为:

  方法1:自己实现的

View Code
  1 //(1)形参和实参均为数组名
  2 #include<stdio.h>
  3 #include<string.h>
  4 void loopmove(char temp[],int n)
  5 {
  6     int i=0,j,len;
  7     char c;
  8     len=strlen(temp);
  9     for(i=0;i<n;i++)
 10     {
 11         c=temp[len-1];
 12         for(j=len-2;j>=0;j--)
 13             temp[j+1]=temp[j];
 14         temp[0]=c;
 15     }
 16 }
 17 void main()
 18 {
 19     char s[]="123456789";
 20     int steps=2;
 21     loopmove(s,steps);
 22     printf("%s\n",s);
 23 }
 24 //--------------------------------------------------------------------------
 25 //(2)形参为指针变量,实参为数组名
 26 #include<stdio.h>
 27 #include<string.h>
 28 void loopmove(char *p,int n)
 29 {
 30     int i,j,len;
 31     char *q, temp;
 32     len=strlen(p);
 33     printf("%d\n",len);
 34 
 35     for(i=0;i<n;i++)
 36     {
 37         q=p;
 38         temp=*(p+len-1);
 39         for(j=len-2;j>=0;j--)
 40         {
 41             *(p+j+1)=*(p+j);
 42         }
 43         *p=temp;
 44     }
 45 }    
 46 void main()
 47 {
 48     char s[]="123456789";
 49     int steps=2;
 50     loopmove(s,steps);
 51     printf("%s\n",s);
 52 }
 53 //--------------------------------------------------------------------------
 54 (3)//形参为指针变量,实参也为指针变量
 55 #include<stdio.h>
 56 #include<string.h>
 57 void loopmove(char *p,int n)
 58 {
 59     int i,j,len;
 60     char *q, temp;
 61     len=strlen(p);
 62 //    printf("%d\n",len);
 63 
 64     for(i=0;i<n;i++)
 65     {
 66         q=p;
 67         temp=*(p+len-1);
 68         for(j=len-2;j>=0;j--)
 69         {
 70             *(p+j+1)=*(p+j);
 71         }
 72         *p=temp;
 73     }
 74 }    
 75 void main()
 76 {
 77     char *p,s[]="123456789";
 78     int steps=2;
 79     p=s;
 80     loopmove(p,steps);
 81     printf("%s\n",p);
 82 }
 83 //------------------------------------------------------------------------
 84 (4)//形参为数组名,实参为指针变量
 85 #include<stdio.h>
 86 #include<string.h>
 87 void loopmove(char temp[],int n)
 88 {
 89     int i=0,j,len;
 90     char c;
 91     len=strlen(temp);
 92     for(i=0;i<n;i++)
 93     {
 94         c=temp[len-1];
 95         for(j=len-2;j>=0;j--)
 96             temp[j+1]=temp[j];
 97         temp[0]=c;
 98     }
 99 }
100     
101 void main()
102 {
103     char *p,s[]="123456789";
104     int steps=2;
105     p=s;
106     loopmove(p,steps);
107     printf("%s\n",p);
108 }

  方法2:利用strcpy函数

View Code
 1 #include<stdio.h>
 2 #include<string.h>
 3 #define max 100
 4 void loopmove(char*pstr,int steps)
 5 {
 6     char temp[max];
 7     int n=strlen(pstr)-steps;
 8     strcpy(temp,pstr+n);
 9     strcpy(temp+steps,pstr);
10     *(temp+strlen(pstr))='\0';
11     strcpy(pstr,temp);
12 }
13 void main()
14 {
15     char *p,s[]="123456789";
16     int steps=2;
17     p=s;
18     loopmove(p,steps);
19     printf("%s\n",p);
20 }

14、5 字符串与子串的若干问题

(1)求一个字符串中连续出现次数最多的子串

View Code

(2)输入一行字符串,找出其中出现的相同且长度最长的字符串,输出它及其首字符的位置。例如“yyabcdabjcabceg”,输出结果应该为abc和3.

View Code
 1 #include<iostream>
 2 #include<string>
 3 using namespace std;
 4 int main()
 5 {
 6     string s,temp;
 7     int i,j,len;
 8     unsigned int m,n;
 9     cout<<"input the string s:"<<endl;
10     cin>>s;
11     len=s.length();
12     for(i=len-1;i>=1;i--)//程序中此处i应该为i>=1,这样对于“abca”这样的字符串可以得出子串为:a  1 ;而原书中为i>1,只能得到子串长度大于等于2的子串。。
13     {
14         for(j=0;j<len;j++)
15         {
16             if(i+j<=len)
17             {
18                 temp=s.substr(j,i);
19                 m=s.find(temp);
20                 n=s.rfind(temp);
21                 if(m!=n)
22                 {
23                     cout<<"the longest substring is\n"<<temp<<"   "<<m+1<<endl;
24                     return 0;
25                 }
26             }
27         }
28     }
29     cout<<"cannot find the longest substring"<<endl;
30     return 0;
31 }

 (3)写一个函数来模拟C++中的strstr()函数:该函数的返回值是主串中字符子串的位置以后的所有字符。不适用任何C程序已有的函数来完成。

View Code
 1 //方法一:
 2 #include<iostream>
 3 using namespace std;
 4 #define max 20
 5 void strstr1(const char*string,const char*strcharset)
 6 {
 7     int i=0,j=0,flag=0;
 8     while(string[i]!='\0')
 9     {
10         while(string[i]!=strcharset[0])
11             i++;
12         while(strcharset[j]!='\0')
13         {
14             if(string[i++]!=strcharset[j++])
15             {
16                 flag=0;
17                 j=0;
18                 break;
19             }
20         }
21         if(strcharset[j]=='\0')
22         {
23             flag=1;
24             break;
25         }
26     }
27     if(flag==1)
28     {
29     const char*p=&string[i-j];
30         cout<<"the substring is:  "<<p<<endl;
31     }
32 }
33 void main()
34 {
35     char *p="12345554555123";
36     char substr[max];
37     cout<<"please input the substr:"<<endl;
38     cin>>substr;
39     strstr1(p,substr);
40 }
41 //--------------------------------------------------------------------------
42 方法二:
43 #include<iostream>
44 using namespace std;
45 #define max 20
46 const char* strstr1(const char*string,const char*strcharset)
47 {
48     int i,j,temp;
49     for(i=0;string[i]!='\0';i++)
50     {
51         j=0;
52         temp=i;
53         if(string[i]==strcharset[j])
54         {
55             while(strcharset[j++]==string[i++])
56             {
57                 if(strcharset[j]=='\0')
58                     return(&string[i-j]);
59             }
60         i=temp;
61         }
62     }
63     return (NULL);
64 }
65 void main()
66 {
67     const char *q;
68     char *p="12345554555123";
69     char substr[max];
70     cout<<"please input the substr:"<<endl;
71     cin>>substr;
72     q=strstr1(p,substr);
73     cout<<"the substring is: "<<q<<endl;
74 }

(4)实现一个函数将一句话里的单词进行倒置,标点符号不倒换。比如一句话“i come from wuhan.“倒置后变成"wuhan. from come i"。

View Code
 1 #include<stdio.h>
 2 #include<string.h>
 3 #define max 100
 4 void main()
 5 {
 6     int i,j=0,len,m=0,n=0;
 7     char charstr[max]="welcome  to china.",temp;
 8 
 9     len=strlen(charstr);
10     for(i=0;i<len/2;i++)
11     {
12         temp=charstr[i];
13         charstr[i]=charstr[len-1-i];
14         charstr[len-1-i]=temp;
15     }
16     printf("after 1th inverse:  %s\n",charstr);
17 
18     while(charstr[j]!='\0')
19     {
20         if(charstr[j]!=' ')
21         {
22             m=j;
23             while(charstr[j]!='\0' && charstr[j]!=' ')
24                 j++;
25             n=j-1;
26         }
27         while(n>m)
28         {
29             temp=charstr[m];
30             charstr[m]=charstr[n];
31             charstr[n]=temp;
32             m++;
33             n--;
34         }
35         j++;
36     }
37     printf("after 2th inverse:  %s\n",charstr);
38 }

(5)写一个函数,计算4 000 000 000以内的最大的那个f(n)=n的值,函数f的功能是统计所有0到n之间所有含有数字1的数字和。

  比如f(13)=6

  因为1在1,2,3,4,5,6,7,8,9,10,11,12,13中的总数是6(1,11,12,13)

(6)转换字符串格式为原来字符串的字符+该字符出现的个数,例如字符串1233422222,转化为1121324125.

  主要收获是sprintf函数的使用:http://baike.baidu.com/view/1295144.htm

View Code
 1 #include<stdio.h>
 2 #include<string.h>
 3 #define max 100
 4 void main()
 5 {
 6     int i,len,offset=0,count=1;
 7     char str[max];
 8     char temp[max];
 9     printf("please input the string");
10     scanf("%s",str);
11     len=strlen(str);
12     for(i=0;i<len;i++)
13     {
14         if(str[i+1]==str[i])
15         {
16             count++;
17         }
18         else
19         {
20             offset+=sprintf(temp+offset,"%c%d",str[i],count);
21             count=1;
22         }
23     }
24     temp[offset]='\0';
25     printf("the result is:  %s",temp);
26 }
原文地址:https://www.cnblogs.com/lpshou/p/2536799.html