您的位置:首页 > 其它

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

2013-09-19 21:43 459 查看
以前常用的一些字符串处理函数这篇文中都包括了,很有用,所以转载了。文章转自:http://www.cnblogs.com/lpshou/archive/2012/06/05/2536799.html

1、memcpy、memmove、memset源码



link:http://note.youdao.com/share/?id=1f826e4337c7db272e94fdb4f267a8de&type=note



2、strcpy、strcat等源码



link:http://note.youdao.com/share/?id=d23a598b2e31321517ed57d2599de181&type=note



3、atoi和itoa源码:



link:http://note.youdao.com/share/?id=96b713b249981aa0c5f9be5d0657fb90&type=note



整数字符串的转化

1、直接采用现有函数

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

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

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

  具体实现为:
#include<stdlib.h>
#include<stdio.h>
void main()
{
    int a=123;
    char string[10];
    itoa(a,string,10);
    printf("整数:%d\n字符串:%s",a,string);
}


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

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

 函数说明: 参数nptr字符串,如果第一个非空格字符不存在或者不是数字也不是正负号则返回零,否则开始做类型转换,之后检测到非数字(包括结束符 \0) 字符时停止转换,返回整型数。

  具体实现为:
#include<stdlib.h>
#include<stdio.h>
void main()
{
    int a;
    char string[10]="123";
    a=atoi(string);
    printf("转换后的整数:%d",a);
}


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

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

  具体实现为:
#include<stdlib.h>
#include<stdio.h>
void main()
{
    int i,j=0,a=123456;
    char temp[10],string[10];
    while(a)
    {
        i=a%10+'0';
        temp[j++]=i;
        a=a/10;
    }
    i=0;
    j--;
    while(j>=0)
        string[i++]=temp[j--];
    string[i]='\0';
    printf("转换成的字符串为:%s",string);
}


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

  具体实现为:
#include<stdlib.h>
#include<stdio.h>
#include<string>
void main()
{
    char temp[10],string[10]="123456";
    int i=0,j,a=0;
    strcpy(temp,string);
    while(temp[i])
    {
        a=a*10+(temp[i]-'0');
        i++;
    }
    printf("转换为整数:%d",a);
}


14、2
字符数组和strcpy

(1)面试题1

  strcpy字符串拷贝函数的实现

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

  具体实现为:
#include<stdlib.h>
#include<stdio.h>
char *strcpy1(char *strDest,char *strSrc)
{
    char *temp=strDest;
    while((*strDest=*strSrc)!='\0')
    {
        strDest++;
        strSrc++;
    }
    return temp;
}

void main()
{
    char str[]="123iamhappy!";
    char strDest[50];
    strcpy1(strDest,str);
    printf("最终的结果为:%s\n",strDest);    
}


(2)面试题3

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

  具体实现为:

  方法1:自己实现的
//(1)形参和实参均为数组名
#include<stdio.h>
#include<string.h>
void loopmove(char temp[],int n)
{
    int i=0,j,len;
    char c;
    len=strlen(temp);
    for(i=0;i<n;i++)
    {
        c=temp[len-1];
        for(j=len-2;j>=0;j--)
            temp[j+1]=temp[j];
        temp[0]=c;
    }
}
void main()
{
    char s[]="123456789";
    int steps=2;
    loopmove(s,steps);
    printf("%s\n",s);
}
//--------------------------------------------------------------------------
//(2)形参为指针变量,实参为数组名
#include<stdio.h>
#include<string.h>
void loopmove(char *p,int n)
{
    int i,j,len;
    char *q, temp;
    len=strlen(p);
    printf("%d\n",len);

    for(i=0;i<n;i++)
    {
        q=p;
        temp=*(p+len-1);
        for(j=len-2;j>=0;j--)
        {
            *(p+j+1)=*(p+j);
        }
        *p=temp;
    }
}    
void main()
{
    char s[]="123456789";
    int steps=2;
    loopmove(s,steps);
    printf("%s\n",s);
}
//--------------------------------------------------------------------------
(3)//形参为指针变量,实参也为指针变量
#include<stdio.h>
#include<string.h>
void loopmove(char *p,int n)
{
    int i,j,len;
    char *q, temp;
    len=strlen(p);
//    printf("%d\n",len);

    for(i=0;i<n;i++)
    {
        q=p;
        temp=*(p+len-1);
        for(j=len-2;j>=0;j--)
        {
            *(p+j+1)=*(p+j);
        }
        *p=temp;
    }
}    
void main()
{
    char *p,s[]="123456789";
    int steps=2;
    p=s;
    loopmove(p,steps);
    printf("%s\n",p);
}
//------------------------------------------------------------------------
(4)//形参为数组名,实参为指针变量
#include<stdio.h>
#include<string.h>
void loopmove(char temp[],int n)
{
    int i=0,j,len;
    char c;
    len=strlen(temp);
    for(i=0;i<n;i++)
    {
        c=temp[len-1];
        for(j=len-2;j>=0;j--)
            temp[j+1]=temp[j];
        temp[0]=c;
    }
}
    
void main()
{
    char *p,s[]="123456789";
    int steps=2;
    p=s;
    loopmove(p,steps);
    printf("%s\n",p);
}


方法2:利用strcpy函数

#include<stdio.h>
#include<string.h>
#define max 100
void loopmove(char*pstr,int steps)
{
    char temp[max];
    int n=strlen(pstr)-steps;
    strcpy(temp,pstr+n);
    strcpy(temp+steps,pstr);
    *(temp+strlen(pstr))='\0';
    strcpy(pstr,temp);
}
void main()
{
    char *p,s[]="123456789";
    int steps=2;
    p=s;
    loopmove(p,steps);
    printf("%s\n",p);
}


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

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


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

#include<iostream>
#include<string>
using namespace std;
int main()
{
    string s,temp;
    int i,j,len;
    unsigned int m,n;
    cout<<"input the string s:"<<endl;
    cin>>s;
    len=s.length();
    for(i=len-1;i>=1;i--)//程序中此处i应该为i>=1,这样对于“abca”这样的字符串可以得出子串为:a  1 ;而原书中为i>1,只能得到子串长度大于等于2的子串。。
    {
        for(j=0;j<len;j++)
        {
            if(i+j<=len)
            {
                temp=s.substr(j,i);
                m=s.find(temp);
                n=s.rfind(temp);
                if(m!=n)
                {
                    cout<<"the longest substring is\n"<<temp<<"   "<<m+1<<endl;
                    return 0;
                }
            }
        }
    }
    cout<<"cannot find the longest substring"<<endl;
    return 0;
}


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

//方法一:
#include<iostream>
using namespace std;
#define max 20
void strstr1(const char*string,const char*strcharset)
{
    int i=0,j=0,flag=0;
    while(string[i]!='\0')
    {
        while(string[i]!=strcharset[0])
            i++;
        while(strcharset[j]!='\0')
        {
            if(string[i++]!=strcharset[j++])
            {
                flag=0;
                j=0;
                break;
            }
        }
        if(strcharset[j]=='\0')
        {
            flag=1;
            break;
        }
    }
    if(flag==1)
    {
    const char*p=&string[i-j];
        cout<<"the substring is:  "<<p<<endl;
    }
}
void main()
{
    char *p="12345554555123";
    char substr[max];
    cout<<"please input the substr:"<<endl;
    cin>>substr;
    strstr1(p,substr);
}
//--------------------------------------------------------------------------
方法二:
#include<iostream>
using namespace std;
#define max 20
const char* strstr1(const char*string,const char*strcharset)
{
    int i,j,temp;
    for(i=0;string[i]!='\0';i++)
    {
        j=0;
        temp=i;
        if(string[i]==strcharset[j])
        {
            while(strcharset[j++]==string[i++])
            {
                if(strcharset[j]=='\0')
                    return(&string[i-j]);
            }
        i=temp;
        }
    }
    return (NULL);
}
void main()
{
    const char *q;
    char *p="12345554555123";
    char substr[max];
    cout<<"please input the substr:"<<endl;
    cin>>substr;
    q=strstr1(p,substr);
    cout<<"the substring is: "<<q<<endl;
}


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

#include<stdio.h>
#include<string.h>
#define max 100
void main()
{
    int i,j=0,len,m=0,n=0;
    char charstr[max]="welcome  to china.",temp;

    len=strlen(charstr);
    for(i=0;i<len/2;i++)
    {
        temp=charstr[i];
        charstr[i]=charstr[len-1-i];
        charstr[len-1-i]=temp;
    }
    printf("after 1th inverse:  %s\n",charstr);

    while(charstr[j]!='\0')
    {
        if(charstr[j]!=' ')
        {
            m=j;
            while(charstr[j]!='\0' && charstr[j]!=' ')
                j++;
            n=j-1;
        }
        while(n>m)
        {
            temp=charstr[m];
            charstr[m]=charstr
;
            charstr
=temp;
            m++;
            n--;
        }
        j++;
    }
    printf("after 2th inverse:  %s\n",charstr);
}


(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
#include<stdio.h>
#include<string.h>
#define max 100
void main()
{
    int i,len,offset=0,count=1;
    char str[max];
    char temp[max];
    printf("please input the string");
    scanf("%s",str);
    len=strlen(str);
    for(i=0;i<len;i++)
    {
        if(str[i+1]==str[i])
        {
            count++;
        }
        else
        {
            offset+=sprintf(temp+offset,"%c%d",str[i],count);
            count=1;
        }
    }
    temp[offset]='\0';
    printf("the result is:  %s",temp);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: