字符串顺序存储

// ConsoleApplication10.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include"iostream"
using namespace std;

#define MAX 100

typedef struct
{
    char str[MAX];
    int length;
}sqe_str;

sqe_str *init(sqe_str *p)
{
    p->length = 0;
    return p;
}

sqe_str *insert(sqe_str *p, sqe_str s)
{
    if (p->length + s.length >= MAX)
        cout << "It is too large!" << endl;
    else
    {
        for (int i = 0; i < s.length; i++)
        {
            p->str[p->length] = s.str[i];
            p->length++;
        }
    }
    return p;
}

int print(sqe_str *p)
{
    if (p->length == 0)
        cout << "It is empty" << endl;
    else
    for (int i = 0; i < p->length; i++)
        cout << p->str[i];
    cout << endl;
}


int _tmain(int argc, _TCHAR* argv[])
{
    sqe_str s1, s2;
    sqe_str *p1 = &s1;
    sqe_str *p2 = &s2;

    p1 = init(p1);
    p2 = init(p2);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/butchert/p/12123257.html