/* 請實現(xiàn)一個函數(shù),將一個字符串中的空格替換成“%20”。 例如,當(dāng)字符串為We Are Happy.則經(jīng)過替換之后的字符串為 We%20Are%20Happy。 */ #define _CRT_SECURE_NO_WARNINGS #include <iostream> using namespace std; class Solution { public: void replaceSpace(char *str, int length) { for (int i = 0; i < length; ++i){ if (*(str + i) == ' '){ length += 2; memset(str + length-2, 0, 2); for (int j = length-1; j > i; --j){ *(str + j) = *(str + j - 2); } *(str + i) = '%'; *(str + i + 1) = '2'; *(str + i + 2) = '0'; ++i; ++i; } } *(str + length) = '\0'; } }; void foo() { char str[100] = "We Are Happy"; int len = strlen(str); Solution sol; sol.replaceSpace(str, len); cout << str << endl; //如果返回時,str數(shù)組長度出現(xiàn)了變化,就會出現(xiàn)Stack around the variable 'str' was corrupted } int main() { foo(); return EXIT_SUCCESS; }
文章名稱:【字符串】替換空格
本文鏈接:http://chinadenli.net/article12/gjecdc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、Google、外貿(mào)建站、定制網(wǎng)站、App設(shè)計、網(wǎng)頁設(shè)計公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)