字符串编辑

2023-12-13 06:07:16

本来是想要用字符一个一个做的,但是老师给的程序代码框架里面是用字符串来操作,心想试一下也不错,就做了一下,方便是挺方便的,但是用字符一个一个操作可以节省一些空间

程序代码框架如下:

//-----------------程序结构-------------------
// ↓↓↓↓ 提交全部代码 ↓↓↓↓
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
 
//@{你的代码:完成count, del 和 insert 函数的定义和实现}
char * replace(char * sten, char *str1, char *str2){
    int len1 = strlen(str1), len2 = strlen(str2);
    char rstr[200], * p, * start;
    start = sten;
    while(1){
        p = strstr(start, str1);//(1)
        if(p == NULL) break;
        strcpy(rstr, p+len1);   //(2)
        strcpy(p, str2);        //(3)
        strcpy(p+len2, rstr);   //(4)
        start = p+len2;         //(5)
    }
    return sten;
}
 
int main(void) {
 
    char stence[200], cmd[100];
    char * pstr1, * pstr2;
     
    int i = 3;
    gets(stence), gets(cmd);
    pstr1 = cmd+2;
     
    while(1){
        if(cmd[i] == ' '){
            cmd[i] = '\0', pstr2 = cmd+i+1;
            break;
        }else if(cmd[i] == '\0'){
            pstr2 = NULL;
            break;
        }
        i++;
    }
     
    switch(cmd[0]){
        case 'C': printf("%d\n", count(stence, pstr1));break;
        case 'D': printf("%s\n", del(stence, pstr1));break;
        case 'I': printf("%s\n", insert(stence, pstr1, pstr2));break;
        case 'R': printf("%s\n", replace(stence, pstr1, pstr2));
    }
    return 0;
}   
   

听说gets已经在C++里面被淘汰了(因为是一直读取直到读到换行符)

要求也没有写如果没出现目标子串要怎么办,有点烦,结果是如果没有的话就不进行操作(所以程序还是得写得有鲁棒性)

代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
 
//@{你的代码:完成count, del 和 insert 函数的定义和实现}
int count(char * sten, char * str)
{
    char * p = sten;
    int n = 0, len = (int)strlen(str);
    while(1)
    {
        p = strstr(p, str);
        if(p == NULL)  break;
        n++, p += len;
    }
    return n;
}
char * del(char * sten, char * str)
{
    int len = strlen(str);
    char rstr[200], * p;
    p = strstr(sten, str);
    if(p == NULL)  return sten;
    strcpy(rstr, p + len);
    strcpy(p, rstr);

    return sten;
}
char * insert(char * sten, char * str1, char * str2)
{
    int len1 = strlen(str1), len2 = strlen(str2);
    char rstr[200], * p_h, * p_e;
    p_h = p_e = sten;
    while(1)
    {
        p_e = strstr(p_e, str1);
        if(p_e == NULL)  break;
        p_h = p_e;
        if(len1 >= (int)strlen(p_e))  break;
        p_e += len1;
    }
    if(p_h == sten)  return sten;
    strcpy(rstr, p_h);
    strcpy(p_h, str2);
    strcpy(p_h + len2, rstr);

    return sten;
}
char * replace(char * sten, char *str1, char *str2){
    int len1 = strlen(str1), len2 = strlen(str2);
    char rstr[200], * p, * start;
    start = sten;
    while(1){
        p = strstr(start, str1);//(1)
        if(p == NULL) break;
        strcpy(rstr, p+len1);   //(2)
        strcpy(p, str2);        //(3)
        strcpy(p+len2, rstr);   //(4)
        start = p+len2;         //(5)
    }
    return sten;
}
 
int main(void) {
 
    char stence[200], cmd[100];
    char * pstr1, * pstr2;
     
    int i = 3;
    gets(stence), gets(cmd);
    pstr1 = cmd+2;
     
    while(1){
        if(cmd[i] == ' '){
            cmd[i] = '\0', pstr2 = cmd+i+1;
            break;
        }else if(cmd[i] == '\0'){
            pstr2 = NULL;
            break;
        }
        i++;
    }
     
    switch(cmd[0]){
        case 'C': printf("%d\n", count(stence, pstr1));break;
        case 'D': printf("%s\n", del(stence, pstr1));break;
        case 'I': printf("%s\n", insert(stence, pstr1, pstr2));break;
        case 'R': printf("%s\n", replace(stence, pstr1, pstr2));
    }
    return 0;
}

文章来源:https://blog.csdn.net/Fool256353/article/details/134956861
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。