串的顺序存储

#include <iostream>
using namespace std;

#define MaxStrSize 256
typedef struct mystring
{
    char str[MaxStrSize];
    int len;
}MyString;
    
/*
    掩饰符号类型。例如当需要定义多个包含80个元素的数组a,b,c时,可以这样定义: 
    typedef char Array_eighty[80]; 
    Array_eighty a,b,c; 

    也可以这样隐藏指针: 
    typedef char * pstr; 
    int mystrcmp(pstr, pstr);
*/
//求串的长度
int StrLength(MyString &S)
{
    int i=0;
    while(S.str[i]!='\0')
        i++;
    S.len=i;
    return S.len;
}
//打印串
void StrPrint(MyString &S)
{
    if(S.len<=0)
    {
       cout<<"空串!"<<endl;
    }
    else
    {
       int i=0;
       while(S.str[i] != '\0')
       {
            cout<<S.str[i];
            i++;
       }
    }
}
//串复制
/*
void StrCopy(MyString &to,MyString &from)
{ 
    int i;
    for(i=0; i<from.len; i++)
    {
       to.str[i] = from.str[i];
    }
    to.len = from.len;
    to.str[to.len] = '\0';
}
*/
//串连接
/*
void Strcat(MyString &to,MyString &from)
{
    int i;
    for(i=0; i<from.len; i++)
    {
       to.str[to.len+i] = from.str[i];
    }
    to.len=to.len+from.len;
    to.str[to.len] = '\0';
}
*/
//串比较
/*
int strcmp(MyString &s1,MyString &s2)
{
    int i;
    for(i=0; i<=s1.len; i++)
    {
       if(s1.str[i]!=s2.str[i])
           return s1.str[i]-s2.str[i];               
    }
        return 0;
}
*/
//字符定位
/*
void StrLocate(MyString &s1,char c)
{
    int i;
    for(i=0; i<s1.len; i++)
    {
       if(s1.str[i]==c)
       {
           cout<<"位置:"<<i<<endl;
           break;
       }
    }
    if(i==s1.len)
        cout<<"查无此人!"<<endl;
}
*/
//求s中从第index个字符开始长度为len的子串
/*
MyString StrSub(MyString &s, int index, int len)
{
    MyString temp;
    temp.len=0;
    if(index+len>s.len)
    {
        cout<<"提取的子串过长!"<<endl;
    }
    else
    {
        int i;
        for(i=0;i<len;i++)
            temp.str[i]=s.str[index+i];
        temp.len=len;
        temp.str[temp.len] = '\0';
    }
    return temp;
}
*/
//删除s中从第index个字符开始长度为len的子串
/*
void StrDel(MyString &s, int index, int len)
{
    int i;
    if(index+len>s.len)
        s.len=index;
    else
    {
        for(i=index+len;i<s.len;i++)
            s.str[i-len]=s.str[i];
        s.len=s.len-len;
    }
    s.str[s.len]='\0';
}
*/
//向串s中第index个位置插入串t
void StrInsert(MyString &s, int index, MyString &t)
{
    if(index>s.len)
        cout<<"插入位置不对!"<<endl;
    else
    {
        int i,j=1;
        for(i=s.len+t.len-1;i>=index+t.len;i--)
        {
            s.str[i]=s.str[s.len-j];
            j++;
        }
        for(i=0;i<t.len;i++)
        {
            s.str[index+i]=t.str[i];
        }
        s.len=s.len+t.len;
        s.str[s.len]='\0';
    }
}
//s串中从第 index 个字符开始的 len 个连续字符将被 t 替换
void StrRep(MyString &s, int index, int len, MyString &t)
{
    int i;
    for(i=0;i<len;i++)
        s.str[index+i]=t.str[i];
    s.len=(index+i)>s.len ? (index+i) : s.len;
    s.str[s.len]='\0';
}
int main()
{
    MyString S1={"Hello World!",StrLength(S1)};
    cout<<S1.len<<endl;
    StrPrint(S1);
    MyString S2={"世界你好",StrLength(S2)};
    cout<<endl<<S2.len<<endl;
    StrPrint(S2);
    cout<<endl;
    //复制串1到串3
    //MyString S3;
    //StrCopy(S3,S1);
    //cout<<endl<<S3.len<<endl;
    //StrPrint(S3);
    //cout<<endl;

    //串2连接到串1
    //Strcat(S1,S2);
    //cout<<S1.len<<endl;
    //StrPrint(S1);

    //串1和串2比较
    /*
    int result=strcmp(S1,S2);
    if(result>0)
        cout<<"串1大于串2"<<endl;
    else if(result==0)
        cout<<"串1等于串2"<<endl;
    else
        cout<<"串1小于串2"<<endl;
    */
    
    //字符定位
    //char ch;
    //cin>>ch;
    //StrLocate(S1,ch);
    
    //提取S1中的子串
    //MyString sub1;
    //sub1=StrSub(S1, 6, 5);
    //cout<<sub1.len<<endl;
    //StrPrint(sub1);
    //cout<<endl;

    //删除S1中的子串
    //StrDel(S1, 5, 8);
    //cout<<S1.len<<endl;
    //StrPrint(S1);
    //cout<<endl;

    //向串S1中第index个位置插入串t
    StrInsert(S1, 5, S2);
    cout<<S1.len<<endl;
    StrPrint(S1);
    cout<<endl;
    
    //替换S1串中从第 index 个字符开始的 len 个连续字符
    MyString T={"********",StrLength(S1)};
    StrRep(S1, 16, 8, T);
    cout<<S1.len<<endl;
    StrPrint(S1);
    cout<<endl;
    return 0;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

minyuanxiani

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值