剑指offer || 03_replaceBlank

来源:互联网 发布:智能黑科技软件 编辑:程序博客网 时间:2024/05/19 20:18
#include"string.h"#include<iostream>using namespace std;void ReplaceBlank(char str[]){if (strlen(str) == 0)return;//find num of blankint NumOfBlank = 0;for (int i = 0; i < strlen(str); i++){if (str[i] == ' ')NumOfBlank++;}int j = strlen(str) + NumOfBlank * 2;int i = strlen(str) - 1;str[j--] = '\0';while (i >= 0){if (str[i] == ' '){str[j--] = '0';str[j--] = '2';str[j--] = '%';}else{str[j--] = str[i];}i--;}return;}int main(){char b[50] = { " " };ReplaceBlank(b);return 0;}

0 0