请实现一个函数,把字符串中的每个空格替换成“%20”。例如输入“we are happy.”,则输出“we%20are%20happy.”
#include<stdio.h> #include<stdlib.h> #include<string.h> int main() { char arr[] = "we are happy"; int i = 0; int j = 0; int len = 0; int count = 0; len = strlen(arr); for (i = 0; i < len; i++) { if (arr[i] == ' ') { count++; } } i = len; j = 2 * count + len; while (i != j != 0) { if (arr[i] == ' ') { arr[j--] = '0'; arr[j--] = '2'; arr[j--] = '%'; i--; } else { arr[j] = arr[i]; j--; i--; } } len = strlen(arr); for (i = 0; i < len; i++) { printf("%c", arr[i]); } printf("\n"); system("pause"); return 0; }
we%20are%20happy
请按任意键继续. . .