Skip to content

a%20b%20c%20d *** stack smashing detected ***: ./004 terminated 已放弃 (核心已转储) #8

@MrLinNing

Description

@MrLinNing

``
#include

using namespace std;

#define __tmain main

class Solution
{
public:
void replaceSpace(char *str,int length)
{
int i, j;
int count = 0;
int len = length;
for(int i = 0; i < length; i++)
{
if(str[i] == ' ')
{
count++;
}
}

    len = length + count * 2;
    for(i = length - 1, j = len - 1;
        i >= 0 && j >= 0;)
    {
        if(str[i] == ' ')
        {
            str[j--] = '0';
            str[j--] = '2';
            str[j--] = '%';
            i--;
        }
        else
        {
            str[j--]  = str[i--];
        }
    }
    str[len] = '0';

}

};

int __tmain( )
{
char str[10 + 1] = "a b c d";

Solution solu;
solu.replaceSpace(str, 10);

cout <<str <<endl;

return 0;

}
``
char str[10 + 1] = "a b c d";
换成:
char str[7 + 1] = "a b c d";
报错:
a%20b%20c%20d
*** stack smashing detected ***: ./004 terminated
已放弃 (核心已转储)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions