gatieme / CodingInterviews

剑指Offer——名企面试官精讲典型编程题

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

MrLinNing opened this issue · comments

``
#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
已放弃 (核心已转储)