Created
July 11, 2013 10:40
-
-
Save luoxiaoxun/5974439 to your computer and use it in GitHub Desktop.
题目描述:
请实现一个函数,将一个字符串中的空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。 输入:
每个输入文件仅包含一组测试样例。 对于每组测试案例,输入一行代表要处理的字符串。 输出:
对应每个测试案例,出经过处理后的字符串。 样例输入:We Are Happy样例输出:We%20Are%20Happy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
int main(){ | |
char ret[1000000]; | |
gets(ret); | |
int originalLen=0; | |
int numberOfBlank=0; | |
int i=0; | |
while(ret[i]!='\0'){ | |
originalLen++; | |
if(ret[i]==' ') numberOfBlank++; | |
i++; | |
} | |
int newLen=originalLen+numberOfBlank*2; | |
int originalIndex=originalLen-1; | |
int newIndex=newLen-1; | |
while(originalIndex>=0&&newIndex>originalIndex){ | |
if(ret[originalIndex]==' '){ | |
ret[newIndex--]='0'; | |
ret[newIndex--]='2'; | |
ret[newIndex--]='%'; | |
}else{ | |
ret[newIndex--]=ret[originalIndex]; | |
} | |
originalIndex--; | |
} | |
printf("%s\n",ret); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment