字符串的基本操作和模式匹配

#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
#define OK 1
#define ERROR -1
typedef int Status;

字符串的结构体

typedef struct
{
    char ch[MAX_SIZE];
    int length;
}String;

字符串长度初始化为0

Status StrInit(String *S)
{
    S->length = 0;
    return OK;
}

生成一个值等于char的串

Status StrAssign(String *S, char chars[])
{
    int i;

    for(i = 0; chars[i] != '\0'; i++)
    {
        S->ch[i] = chars[i];
        S->length++;
    }

    return OK;
}

输出字符串

void StrOutPut(String S)
{
    int i;

    for(i = 0; i < S.length; i++)
    {
        printf("%c", S.ch[i]);
    }
    printf("\n");
}

创建一个字符串

void StrCreate(String *S)
{
    char ch[MAX_SIZE];
    int i;

    S->length = 0;
    printf("Please enter a string:\n");
    scanf("%s",ch);

    for(i = 0; ch[i] != '\0'; i++)
    {
        S->ch[i] = ch[i];
        S->length++;
    }
}

比较两个字符串的大小
若返回正数,则S>S1
若返回负数,则S

Status StrCompare(String *S, String *S1)
{
    int i;

    for(i = 0; i < S->length && i < S1->length; i++)
    {
        if(S->ch[i] != S1->ch[i])
        {
            return S->ch[i] - S1->ch[i];
        }
    }
    return S->length - S1->length;//若长度短的字符串已经比较完(例如:abcdefg和abcd)则长度长的字符串大  或  两字符串的相等(例如:ggjghsg和ggjghsg)
}

连接两个字符串

Status Concat(String *S1, String *S2, String *S)
{
    int i;
    int j;

    for(i = 0; i < S1->length; i++)
    {
        S->ch[i] = S1->ch[i];
        S->length++;
    }
    for(j = 0; j < S2->length; j++)
    {
        S->ch[i] = S2->ch[j];
        S->length++;
        i++;
    }

    return OK;
}

求字符串的长度

void StrLength(String *S)
{
    printf("The length of the string is :%d\n", S->length);
    printf("\n");
}

求字符串的子串
pos是子串起始位置
len是子串长度

Status SubString(String S, int pos, int len)
{
    int i;

    if(pos < 0 || pos > S.length)//起始位置小于零或大于字符串的长度
    {
        printf("Irregular position.\n");
    }
    else if( pos + len > S.length)//字符串从pos开始到结束没有len位
    {
        printf("It's too long.\n");
    }
    else
    {
        for(i = pos - 1; i <= pos + len - 2; i++)
        {
            printf("%c", S.ch[i]);
        }
        printf("\n");
    }

    return OK;
}

字符串的模式匹配

Status Index(String S, int pos, String T)
{
    int i;
    int j;
    int tag =0;//标记位(遇到重复的字符时)
    int posi = 0;//返回子串的起始位置

    if(pos < 0 || pos >= S.length)
    {
        printf("Irregular position.\n");
    }
    else
    {
        i = pos - 1;
        j = 0;
        while(i < S.length && j < T.length)
        {
            if(S.ch[i] == T.ch[j])
            {
                if(j == 0)
                {
                    tag = i;
                }
                i++;
                j++;
            }
            else
            {
                if(j != 0)
                {
                    i = tag + 1;
                }
                else
                {
                    i = i + 1;
                }
                posi = i;
                j = 0;
            }
        }
        if(j == T.length)
        {
            printf("success\n");
            printf("position:%d\n", posi + 1);
        }
        else
        {
            printf("fail\n");
        }
    }

    return OK;
}
int main()
{
    int a = 0;
    String S;
    String S1;
    String S2;
    String S3;
    char chars[MAX_SIZE];
    char ch[MAX_SIZE];
    char ch1[MAX_SIZE];

    StrInit(&S);

    printf("1.create a string equals char\n2.create a string\n3.compare S1 with S2\n4.connect two strings\n5.find a substring of length number\n6.string pattern matching\n");
    printf("*****************\n");
    printf("  S  :new string\n");
    printf("S1,S2:S1 compare with S2\n");
    printf("  S3 :S1+S2\n");
    printf("*****************\n");

    while(1)
    {
        printf("Please enter the operation code:");
        scanf("%d", &a);

        if(a == 1)
        {
            int res = 0;

            printf("Please enter a string:\n");
            scanf("%s", chars);
            res = StrAssign(&S, chars);
            if(res == 1)
            {
                printf("Copy success!\n");
                StrOutPut(S);
                StrLength(&S);
            }
            else
            {
                printf("Copy fail!\n");
            }
        }
        else if(a == 2)
        {
            S.ch[0] = '\0';
            S.length = 0;
            StrCreate(&S);
            StrOutPut(S);
            StrLength(&S);
        }
        else if(a == 3)
        {
            int res = 0;

            S1.ch[0] = '\0';
            S1.length = 0;
            S2.ch[0] = '\0';
            S2.length = 0;

            StrCreate(&S1);

            StrCreate(&S2);

            res = StrCompare(&S1, &S2);
            if(res > 0)
            {
                printf("The first string is larger than the the second string.\n");
            }
            else if(res < 0)
            {
                printf("The first string is less than the second string.\n");
            }
            else if(res == 0)
            {
                printf("The first string is equal to the second string.\n");
            }
        }
        else if(a == 4)
        {
            StrInit(&S3);
            S1.ch[0] = '\0';
            S1.length = 0;
            S2.ch[0] = '\0';
            S2.length = 0;
            StrCreate(&S1);
            StrCreate(&S2);
            Concat(&S1, &S2, &S3);
            printf("*************\n");
            StrOutPut(S3);
            StrLength(&S3);
        }
        else if(a == 5)
        {
            int g, h;
            StrCreate(&S);
            printf("Please enter position:");
            scanf("%d", &g);
            printf("Please enter length:");
            scanf("%d", &h);
            SubString(S, g, h);
        }
        else if(a == 6)
        {
            int b;
            String T1;
            String T2;

            StrInit(&T1);
            StrInit(&T2);

            printf("Please enter a string:\n");
            scanf("%s", ch);
            StrAssign(&T1, ch);

            printf("Please enter a string:\n");
            scanf("%s", ch1);
            StrAssign(&T2, ch1);

            printf("Please enter a position:\n");
            scanf("%d", &b);

            Index(T1, b, T2);
        }
        else
        {
            printf("Operation coding error.\n");
            printf("Please enter again.\n");
        }
    }

`
   return 0;
}

运行结果:
这里写图片描述

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值