您的位置:首页 > 其它

cracking the coding interview problem solution 1.5

2013-06-12 18:54 387 查看
#include <stdafx.h>
#include <string.h>
#include <stdio.h>

#define MAXLEN 1024

int main()
{
char pStrInput[MAXLEN];
char *pStr1 = "%20";

//scanf("%s", pStrInput);
char ch;
int i;
for(i = 0; i < MAXLEN && (ch = getchar()) != '\n'; i++)
{
pStrInput[i] = ch;
}
pStrInput[i] = 0;
int iStrLen = strlen(pStrInput);

int iNumSpace = 0;
int iNewLen = 0;
for(int i = 0; i < iStrLen; i++)
{
if(pStrInput[i] == ' ')
iNumSpace++;
}
iNewLen = (strlen(pStr1) - 1) * iNumSpace + iStrLen;
pStrInput[iNewLen] = 0;

int j = iNewLen - 1;
int k = 0;
for(i = iStrLen - 1; i >= 0; i--)
{
if(pStrInput[i] == ' ')
{
for(k = strlen(pStr1) - 1; k >= 0; k--)
pStrInput[j--] = pStr1[k];
}
else
pStrInput[j--] = pStrInput[i];
}

printf("The new string is %s\n", pStrInput);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: