C实例---关键词查找、替换算法

运行环境:macOS shell
代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define     ALL     1
#define     SINGLE  0

int str2num(char *str);  //字符串转宏
void Find(const char *src, char *argv, int Mode, int *position);    //查找函数
char *Replace(char *src, char *argv, char *replace, int Mode, int *position);   //替换函数
void atoA(char *src);   //小写转大写
void PrintInfo(void);   //打印提示信息
void ArrayReverse(int *array);  //数组倒置---冒泡实现
int main ()
{
    char string[1024] = "This year is Chinese new year---the year of the rooster, and happy new year to you!";
    char keyword[15] = {0};
    char mode[10] = {0}, M[10] = {0}, replace[15] = {0};
    int pos[10] = {0};
    int i,count = 0;
    int len;
    int argvlen;

    PrintInfo();

    printf("\n\nTest String :\n");
    printf("%s\n\n", string);

    printf("Please input your find or repalce keyword : ");
    scanf("%[^\n]",keyword);

    getchar();
    printf("Please chose the mode --- \"ALL or SINGLE\": ");
    scanf("%[^\n]",mode);
    atoA(mode);
    /* while input error, resume load up to correct. */
    while ( (strcmp(mode, "ALL") != 0) && (strcmp(mode, "SINGLE") != 0) )
    {
        printf("Line %d : Input Mode Error! Sample : all or ALL or single or SINGLE!\n", __LINE__);
        printf("Resume Mode : ");
        getchar();
        scanf("%[^\n]",mode);
        atoA(mode);
    }
    printf("Debug %d : mode --- %s\n",__LINE__, mode);

    getchar();
    printf("Please chose the mode --- \"FIND or REPLACE\" : ");
    scanf("%[^\n]",M);
    atoA(M);

//    printf("Debug %d : mode --- %s\n",__LINE__, mode);
    /* while input error, resume load up to correct. */
    while ( (strcmp(M, "FIND") != 0) && (strcmp(M, "REPLACE") != 0) )
    {
        printf("Line %d : Input Mode Error! Sample : find or FIND or replace or REPLACE!\n", __LINE__);
        printf("Resume Mode : ");
        getchar();
        scanf("%[^\n]",M);
        atoA(M);
    }
//    printf("Debug %d : M --- %s\n",__LINE__, M);
//    printf("Debug %d : mode --- %s\n",__LINE__, mode);
    if (strcmp(M, "REPLACE") == 0)
    {
        printf("Please input your replace keyword : ");
        getchar();
        scanf("%[^\n]",replace);
    }

//    printf("Debug %d : mode --- %s\n",__LINE__, mode);
//    printf("Debug %d : replace --- %s\n",__LINE__, replace);
    len = (int)strlen(string);
    argvlen = (int)strlen(keyword);
    Find(string, keyword, str2num(mode), pos);

    if (strcmp(M, "FIND") == 0)
    {
        for (i = 0; i < len; i ++)
        {
            if (pos[count] != -1 && pos[count] != -2 && i == pos[count])
            {
                printf("\e[1;30;47m%s\e[m",keyword);
                i += argvlen - 1;
                count ++;
            }
            else
            {
                printf("%c",string[i]);
            }
        }
        printf("\n");
    }
    else if (strcmp(M, "REPLACE") == 0)
    {
        count = 0;
        Replace(string, keyword, replace, str2num(mode), pos);
        len = (int)strlen(string);
        argvlen = (int)strlen(replace);

        for (i = 0; i < len; i ++)
        {
            if (pos[count] != -1 && pos[count] != -2 && i == pos[count])
            {
                printf("\e[1;30;47m%s\e[m",replace);
                i += argvlen - 1;
                count ++;
            }
            else
            {
                printf("%c",string[i]);
            }
        }
        printf("\n");
    }

    return 0;
}

void PrintInfo(void)
{
    printf("******************************************\n");
    printf("*           Find and Repalce Test        *\n");
    printf("*       Sample : <KeyWord> <Mode> <M>    *\n");
    printf("*           Mode : ALL or SINGLE         *\n");
    printf("*           M : FIND or REPALCE          *\n");
    printf("******************************************\n");
}

int str2num(char *str)
{
    int num = 2;
    if (strcmp(str, "ALL") == 0)
        num = 1;
    else if (strcmp(str, "SINGLE") == 0)
        num = 0;

    return num;
}

void Find(const char *src, char *argv, int Mode, int *position)
{
    int i,j,len,argvlen;
    int count = 0;
    len = (int)strlen(src);
    argvlen = (int)strlen(argv);
    switch(Mode)
    {
        case SINGLE:
            if (strstr(src, argv) != NULL)
                *position++ = (int)(strstr(src, argv) - src);
            else
                *position++ = -1;
            break;
        case ALL:
            for (i = 0; i < len; i ++)
            {
                if (src[i] == argv[0])
                {
                    for (j = 1; j < argvlen; j ++)
                    {
                        if (src[i+j] == argv[j])
                        {
                            count ++;
                        }
                    }
                    if (count == argvlen - 1)
                    {
                        *position++ = i;
                        i += argvlen - 1;
                        count = 0;
                    }
                }
            }
            break;
        default:
            printf("Line %d : Mode Error! Sample : \"ALL or SINGLE!\"\n",__LINE__);
            exit(1);
            break;
    }
    *position ++ = -2;
}

char *Replace(char *src, char *argv, char *replace, int Mode, int *position)
{
    char *p = src;
    int *posi = position;
    int len, argvlen,repalcelen,i,j,posm;

    len = (int)strlen(src);
    argvlen = (int)strlen(argv);
    repalcelen = (int)strlen(replace);

    switch(Mode)
    {
        case SINGLE:
            if (*position != -1 && *position != -2)
            {
                if (argvlen == repalcelen)
                {
                    for (i = 0; i < argvlen; i ++)
                    {
                        src[(*position) + i] = replace[i];
                    }
                }
                else if (argvlen > repalcelen)
                {
                    posm = argvlen - repalcelen - 1;
                    for (i = 0; i < repalcelen; i ++)
                    {
                        src[(*position) + i] = replace[i];
                        for (j = *position + repalcelen; src[j]; j ++)
                        {
                            src[j] = src[j + posm];
                        }
                    }
                }
                else
                {
                    posm = repalcelen - argvlen;
                    len = (int)strlen(src);
                    for (i = len; i >= *position; i --)
                        src[i + posm] = src[i];

                    for (i = 0; i < repalcelen; i ++)
                        src[(*position) + i] = replace[i];
                }
            }
            else
            {
                printf("Line %d : No keyword to repalce!!\n",__LINE__);
            }
            break;
        case ALL:
            if (*position != -1 && *position != -2)
            {
                if (argvlen == repalcelen)
                {
                    while(*position != -2)
                    {
                        for (i = 0; i < argvlen; i ++)
                        {
                            src[(*position) + i] = replace[i];
                        }
                        position ++;
                    }
                }
                else if (argvlen > repalcelen)
                {
                    ArrayReverse(position);
                    for (i = 0; i < 6; i ++)
                        printf("%d ", position[i]);
                    printf("\n");
                    posm = argvlen - repalcelen - 1;
                    while (*position != -2)
                    {
                        for (i = 0; i < repalcelen; i ++)
                        {
                            src[(*position) + i] = replace[i];
                            for (j = *position + repalcelen; src[j]; j ++)
                            {
                                src[j] = src[j + posm];
                            }
                        }
                        position ++;
                    }
                }
                else
                {
                    posm = repalcelen - argvlen;
                    ArrayReverse(position);
                    while (*position != -2)
                    {
                        len = (int)strlen(src);
                        for (i = len; i >= *position; i --)
                            src[i + posm] = src[i];

                        for (i = 0; i < repalcelen; i ++)
                            src[(*position) + i] = replace[i];

                        position ++;
                    }

                }
            }
            else
            {
                printf("Line %d : No keyword to repalce!!\n",__LINE__);
            }

            break;
        default:
            printf("Line %d : Mode Error! Sample : \"ALL or SINGLE!\"\n", __LINE__);
            exit(1);
            break;
    }
    Find(src, replace, Mode, posi);
    return p;
}

/*小写字符转大写字符*/
void atoA(char *src)
{
    while (*src != '\0')
    {
        if (*src >= 'a' && *src <= 'z')
            *src -= 32;
        src ++;
    }
}

void ArrayReverse(int *array)
{
    int count = 0,i,j,tmp;
    while (array[count] != -2)
        count ++;

    for (i = count; i >= 0; i --)
    {
        for (j = 0; j < i-1; j ++)
        {
            tmp = array[j];
            array[j]= array[j+1];
            array[j+1] = tmp;
        }
    }
}

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

猜你喜欢

转载自blog.csdn.net/huazhen1234/article/details/54931850