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;
}
#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;
}
相关文章推荐
- cracking the coding interview problem solution 1.8
- cracking the coding interview problem solution 1.3
- Cracking the Coding Interview Q1.5
- Cracking The Coding Interview 3rd -- 1.5*
- cracking the coding interview No1.5
- Cracking The Coding Interview 1.5
- Cracking the Coding Interview(Trees and Graphs)
- Cracking the coding interview--Q1.8
- Cracking the coding interview--Q19.7
- Cracking the coding interview--Q1.1
- Cracking the coding interview--Q1
- Cracking The Coding Interview 3rd -- 1.3
- 《Cracking the Coding Interview》——第8章:面向对象设计——题目7
- 《Cracking the Coding Interview》——第14章:Java——题目3
- 《Cracking the Coding Interview》——第17章:普通题——题目6
- 《Cracking the Coding Interview》——第18章:难题——题目4
- Cracking the coding interview--Q2.3
- 《Cracking the Coding Interview》题目汇总
- Cracking the coding interview--Q2.3
- Cracking the coding interview--Q2.5