m0_65596353 2022-10-19 20:05 采纳率: 78.6%
浏览 30
已结题

在对字符串的插入和删除过程中,” ,不知道为什么输出的删除的结果后面出现了一堆“烫”和别的东西,并且一运行到插入函数就抛出异常“0xC…39处有未经处理的异常……

在输入了要删除的部分的初始位置i和长度m后,输出的结果在正确结果后面多加上了一堆“烫”和别的东西,
然后在输入例如插入的后一个位置i后,就会显示异常“0xCCCCCC39 处有未经处理的异常(在 StringApp.exe 中): 0xC00001A5: 检测到无效的异常处理程序例程。 (参数: 0x00000003)”。
调试是每次到main函数里的“str.strinsert(s,i,T);”就会弹出异常,如下图,代码也放在下面:

img

img

头文件:String.h

#pragma once
class String
{
public:
     void strinsert(char *s, int i, char *T);//字符串T;
     void strdelete(char *s, int i, int m);//长度m;
     int strlen(char *s);
     void strprint(char *s);
};

函数的定义:String.cpp

#include "stdafx.h"
#include "String.h"
#include<iostream>
using namespace std;


int String::strlen(char *s)
{
    int len=0;
    int i=0;
    while(s[i]!='\0')
    {
        len++;
        i++;
    }
    return len;
}

void String::strprint(char *s)
{
    int i;
    for(i=0;i<strlen(s);i++)
    {
        cout<<s[i];
    }
    cout<<endl;
}

void String::strdelete(char *s, int i, int m)
{
    if(m>strlen(s))
        cout<<"没有字符被删除";
    else if((m+i)>=strlen(s))
    {
        s[i]='\0';
        strprint (s);
    }
    else
    {
        for(int a=i;a<(strlen(s)-1-(i+m-1));a++)
        {
            s[a]=s[a+m];
            
        }
    
        strprint (s);
    }
}

void String::strinsert(char *s, int i, char *T)//i是字符串s的第i个位置
{
    s=strcat(s,T);//将T连接到s的后面
    for(int b=0;b<strlen(T);b++)
    {
        int a;
        a=i-strlen(T);
        int c;
        c=strlen(s)-strlen(T);
        int temp;
        temp=s[a];
        s[a]=s[c];
        s[c]=temp;
        a++;
        c++;
    }
    cout<<"插入后的字符串为:"<<endl;
    strprint(s);
}

main函数:

// StringApp.cpp : 定义控制台应用程序的入口点。
#include "stdafx.h"
#include "String.h"
#include<iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
    String str;
    char s[]={'0','1','2','3','4','5','6','7','8','9'};
    char T[]={'m','a','t','h'};
    int i;
    int m;
    cout<<"请输入删除的起始位置索引号i和删除长度m:";
    cin>>i>>m;
    str.strdelete(s,i,m);
    cout<<"请输入插入位置的后一位索引号i:";
    cin>>i;
    int a=0;
    str.strinsert(s,i,T);//在s[i]前插入字符串T
    str.strlen (s);
    return 0;
}
  • 写回答

1条回答 默认 最新

  • 浪客 2022-10-19 21:21
    关注

    出现烫一般是内存越界,检测字符串末尾的'\0'
    能用strcat,实现起来就简单了。

    class String
    {
    public:
        void strinsert(char *s, int i, char *T); //字符串T;
        void strdelete(char *s, int i, int m);     //长度m;
        int strlen(char *s);
        void strprint(char *s);
    };
    
    int String::strlen(char *s)
    {
        int len = 0;
        // int i = 0;
        while (*s++) // while (s[i] != '\0')
        {
            len++;
            // s++; // i++;
        }
        return len;
    }
    
    void String::strprint(char *s)
    {
        // int i;
        while (*s) // for (i = 0; i < strlen(s); i++)
        {
            cout << *s++; // cout << s[i];
        }
        cout << endl;
    }
    
    void String::strdelete(char *s, int i, int m)
    {
        int len = strlen(s);
        if (m > len || i > len) // if (m > strlen(s))//
        {
            cout << "没有字符被删除\n";
            return;
        }
        if ((m + i) >= strlen(s))
        {
            s[i] = '\0';
            strprint(s);
        }
        else
        {
            int a;
            // for (a = i; a < (strlen(s) - 1 - (i + m - 1)); a++) //
            for (a = i; s[a + m]; a++)
            {
                s[a] = s[a + m];
            }
            s[a] = '\0';
    
            strprint(s);
        }
    }
    
    void String::strinsert(char *s, int i, char *T) // i是字符串s的第i个位置
    {
        int lens = strlen(s);
        // strcat(s, T);               //将T连接到s的后面
        if (i <= lens || lens > 0) //索引超出或者s为空,直接输出s
        {
            lens = strlen(s);
            int lenT = strlen(T);
            char *buf = (char *)malloc(lens + 1);
            strcpy(buf, s + i); //保存i后面字符串
            *(s + i) = 0;
            strcat(s, T);
            strcat(s, buf);
            free(buf);
            // for (int b = 0; b < lenT; b++)
            // {
            //     int a;
            //     a = i - lenT; // i和lenT是定值,因此a始终不变
            //     int c;
            //     c = lens - lenT;
            //     int temp;
            //     temp = s[a];
            //     s[a] = s[c];
            //     s[c] = temp;
            //     a++;
            //     c++;
            // }
        }
        else
            strcat(s, T);
        cout << "插入后的字符串为:" << endl;
        strprint(s);
    }
    
    int main()
    {
        String str;
        char s[30] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 0}; //必须有足够的长度容纳T
    
        char T[] = {'m', 'a', 't', 'h', '\0'}; //字符串结束标记要加上
        int i;
        int m;
        cout << "请输入删除的起始位置索引号i和删除长度m:";
        cin >> i >> m;
        str.strdelete(s, i, m);
        cout << "请输入插入位置的后一位索引号i:";
        cin >> i;
        int a = 0;
        str.strinsert(s, i, T); //在s[i]前插入字符串T
        str.strlen(s);
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 10月21日
  • 已采纳回答 10月21日
  • 修改了问题 10月19日
  • 创建了问题 10月19日

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度