码迷,mamicode.com
首页 > 编程语言 > 详细

字符串算法总结

时间:2016-08-11 00:59:09      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:

  • 数字转字符串
  • 字符串逆置(char*、string、折半法逆置字符串)
  • 字符串逆置保持单词不变
  • 实现strcpy,strcat,strcmp,strstr
#include<iostream>
#include<string>
#include<assert.h>
using namespace std;


//数字转字符串
string InttoChar(int num){
    if(num==0)
        return "0";
    string res;
    while(num!=0){
        int tmp=num%10;
        char ctmp=tmp+0;
        res.push_back(ctmp);
        num/=10;
    }
    reverse(res.begin(),res.end());
    return res;
}
//字符串逆置
string reversestring(string s1){
    if(s1=="")
        return s1;
    string s2="";
    for(int i=s1.length()-1;i>=0;i--){
        s2+=s1[i];
    }
    return s2;
}
//折半法逆置字符串
char* revetsestring(char* s1){
    if(s1==NULL)
        return s1;
    int len=strlen(s1);
    char* p1,*p2;
    p1=s1,p2=s1+len-1;
    char tmp;
    while(p1!=p2){
        tmp=*p1;
        *p1=*p2;
        *p2=tmp;

        p1++;
        p2--;
    }
    return s1;
}
//字符串逆置单词内部顺序不变
char* ReverseWords(char* s1){
    int len=strlen(s1);
    if(s1==NULL||len==1)
        return s1;
    int i=len-1,j=len;  //ij记录单词的位置
    int t=0; //暂存已经录入单词的位置
    char* res= new char[len+1]; //len不包含‘\0‘要申请len+1个
    int k=0;  //新字符串的位置
    while(i>0){
        while(s1[i]!= &&i!=0)
            i--;
        t=i;
        if(i!=0)
        {
            i++;
            while(i<j){
                res[k++]=s1[i++];
            }
            res[k++]= ;
            j=t;
            i=t-1;
        }
        else{
            while (i<j)
            {
                res[k++]=s1[i++];
            }
            i=t;
        }

    }
    res[len]=\0;
    return res;
}

//分别实现strcpy,strcat,strcmp,strstr
//strcpy需要一个指针记录开始的位置
char* stringcpy(char* des,char* src){
    if(src==NULL)
        return src;
    int len=strlen(src);
    des=new char[len+1];
    char* res=des;
    while(*src!=\0)
        *des++=*src++;
    *des=\0;
    return res;
}


//strcat,strcat需要des保证可以容纳src,我们这里不需要
char* stringcat(char* des,char*src){
    if(src==NULL)
        return des;
    int len1=strlen(des);
    int len2=strlen(src);
    char* newstr=new char[len1+len2+1];
    char* res=newstr;
    while (*des!=\0)
        *newstr++=*des++;
    while(*src!=\0)
        *newstr++=*src++;
    *newstr=\0;
    return res;
}


//strcmp,使用assert要包含其头文件assert.h不满足assert条件会报错
int stringcmp(char* s1,char* s2){

    assert((s1!=NULL)&&(s2!=NULL));
    while(*s1&&*s2&&(*s1==*s2)){
        s1++;
        s2++;
    }
    return *s1-*s2;
}

//strstr leetcode有原题,做过
bool samestr(char* s1,char* s2){
    if(strlen(s1)!=strlen(s2))
        return false;
    while(*s1){
        if(*s1==*s2){
            s1++;
            s2++;
        }
        else return false;
    }
    return true;
}
char* stringstr(char* s1,char* s2){
    if(s1==NULL||s1==NULL)
        return NULL;
    int len1=strlen(s1);
    int len2=strlen(s2);
    if(len2>len1)
        return NULL;
    if(len1==len2){
        if(samestr(s1,s2))
            return s1;
        else return NULL;
    }
    
    for(int i=0;i<len1;){
        char* tmp=NULL;
        for(int j=0;j<len2;j++)
            tmp+=s1[i+j];
        tmp+=\0;
        if(samestr(tmp,s2))
            return s1+i;
        else i++;
    }
    return NULL;
}






int main(){
    int num=1234569;
    string res=InttoChar(num);
    cout<<res<<endl;

    char* test="guofei is a student in SEU!";
    string test1="helloworld";
    char* test2="happyforever";
    cout<<reversestring(test2)<<endl;
    cout<<reversestring(test1)<<endl;
    cout<<ReverseWords(test)<<endl;

    char* s1,*s2="cpy this string";
    cout<<stringcpy(s2,s2)<<endl;
    cout<<stringcmp(test,test2)<<endl;

    char* s3="auti";
    char* s4="beautiful girl";
    if(stringstr(s3,s4))
        cout<<"fei zi chuan"<<endl;
    else cout<<"zichuan"<<endl;  //this problem is really tricky, i try to cout NULL and i fix bug just for this little error makes me very wordless
    return 0;
}

 

字符串算法总结

标签:

原文地址:http://www.cnblogs.com/LUO77/p/5759241.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!