基于C语言的语法,实现如下字符串基本操作,并写一个main函数测试。不允许调用字符串相关库函数。
串复制Strcopy(&s1, s2)
串比较StrCompare(s1, s2)
求串长StrLength(s1)
串联接Concat(&s1, s2)

求子串SubString((&Sub, S, pos, len);

#include<iostream>
#include <stdlib.h>
#include<stdio.h>
using namespace std;
typedef int Status;
#define Max 20
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef struct//堆分配表示串
{
    char *ch;
    int length;
} HString;
Status CreatHsring(HString &h);//创建字符串
Status PrintHstring(HString h);//输出字符串
Status Stringlength(HString h);//字符串长度
Status Concat(HString &h,HString s1,HString s2);//串联接
Status Strcopy(HString &h,HString s1);//复制串
Status Substring (HString &Sub, HString S, int pos,int len);//用Sub返回串S的第pos个字符起长度为len的子串
Status StrCompare(HString s1,HString s2);//字符串比较



int  main()
{
    HString S,H,T,L;
    cout << "请输入字符串S1(按回车键结束):" << endl;
    CreatHsring(H);
    cout << "现在串中的字符为:" << endl;
    PrintHstring(H);
    Stringlength(H);
    cout << "请再输入字符串S2(按回车键结束):" << endl;
    CreatHsring(T);
    if(StrCompare(H,T) == ERROR)
        cout<<"字符串S1,S2不相同!"<<endl;
    else
        cout<<"字符串S1,S2相同!"<<endl;


    cout << "请输入字符串S3(按回车键结束):" << endl;
    CreatHsring(L);
    cout<<"将s1,s3字符串连接"<<endl;

    Concat(S, H, L);
    cout << "现在串中的字符为:" << endl;
    PrintHstring(S);
    HString Sub;
    int pos, len;
    cout << "请输入截取位置pos及长度len:" << endl;
    cin >> pos >> len;
    Substring(Sub, S, pos, len);
    cout << "截取的子串为:" << endl;
    PrintHstring(Sub);
}


Status CreatHsring(HString &h)
{
    h.length = 0;
    h.ch = (char *)malloc(sizeof(char)*Max);
    int i;
    for(i = 0; i<Max; i++)
    {
        h.ch[i] = getchar();
        h.length++;
        if(getchar() == '\n')
            break;
    }
    return OK;
}

Status PrintHstring(HString h)
{
    int i;
    if(h.length == 0)
    {
        cout<< "空串!"<<endl;
        return ERROR;
    }
    else
    {
        for(i=0; i<h.length; i++)
        {
            cout<<h.ch[i]<<" ";
        }
        cout<<endl;
        return OK;
    }
}

Status Stringlength(HString h)
{
    cout<<"输入的字符串长度: ";
    cout<<h.length<<endl;
    return OK;
}

Status Concat(HString &h,HString s1,HString s2)
{
    if(!(h.ch = (char *)malloc(sizeof(char)*(s1.length+s2.length))))
        exit(OVERFLOW);
    int i;
    for(i=0; i<s1.length; i++)
    {
        h.ch[i] = s1.ch[i];
    }
    h.length = s1.length+s2.length;
    int j;
    for(j = s1.length; j<h.length; j++)
    {
        h.ch[j] = s2.ch[j-s1.length];
    }
    return OK;
}

Status Strcopy(HString &h,HString s1)
{
    if(h.length<s1.length)
        h.ch = (char*)realloc(h.ch,s1.length*sizeof(char));//内存不够,重新分配内存

    h.length = s1.length;
    int i;
    for(i=0; i<h.length; i++)
    {
        h.ch[i] = s1.ch[i];
    }
    return OK;
}

Status Substring (HString &Sub, HString S, int pos,int len)//用Sub返回串S的第pos个字符起长度为len的子串
{
    int i;
    if (pos <1 || pos > S.length)
    {
        cout << "输入的位置有误!" << endl;
        return ERROR;
    }
    if (len<0 || len > S.length - pos + 1)
    {
        cout << "输入的长度有误!" << endl;
        return ERROR;
    }
    if (!len)
    {
        Sub.ch == NULL;
        Sub.length = 0;
    }
    else
    {
        Sub.ch = (char *)malloc(len*sizeof(char));
        for (i = 0; i < len ; i++)
            Sub.ch[i] = S.ch[pos + i - 1];
        Sub.length = len;
    }
    return OK;
}

Status StrCompare(HString s1,HString s2)
{
    int i;
    for(i=0;i<s1.length;i++)
    {
         if(s1.ch[i]!=s2.ch[i])
            return ERROR;
    }
    return OK;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值