Cracking The Coding Interview 3rd -- 1.4
2014-01-13 12:53
423 查看
Write a method to replace all spaces in a string with '%20'. You may assume that the string has sufficient space at the end of the string to hold the additional characters, and that you are given the "true" length of the string. (Note: if implementing
in Java, please use a character array so that you can perform this operation in place).
Solution: A common approach in string manipulation problems is to edit the string starting from the end and work backwards. For this problem, we work through two scan approach. In the first scan, we count how many spaces there are in the
string. This is used to compute how long the final string should be. In the second pass, which is done in reverse order, we actually edit the string.
Header file
#ifndef __QUESTION_1_4_H_CHONG__
#define __QUESTION_1_4_H_CHONG__
class Question1_4 {
public:
void replaceSpaces(char str[], int length);
int run();
};
#endif // __QUESTION_1_4_H_CHONG__
Source file
#include "stdafx.h"
#include <iostream>
#include "Question1_4.h"
using namespace std;
void Question1_4::replaceSpaces(char str[], int length)
{
int numOfSpaces = 0;
for (int i=0; i<length; i++) {
if (str[i]==' ') {
numOfSpaces++;
}
}
int trueLength = length + 2*numOfSpaces;
for (int i=length-1, j=trueLength-1; i>=0; i--, j--) {
if (str[i]!=' ') {
str[j] = str[i];
}
else {
str[j-2] = '%';
str[j-1] = '2';
str[j] = '0';
j = j - 2;
}
}
}
int Question1_4::run()
{
char str[100] = "Hello Chong!";
int length = 12;
replaceSpaces(str, length);
cout << str << endl;
return 0;
}
in Java, please use a character array so that you can perform this operation in place).
Solution: A common approach in string manipulation problems is to edit the string starting from the end and work backwards. For this problem, we work through two scan approach. In the first scan, we count how many spaces there are in the
string. This is used to compute how long the final string should be. In the second pass, which is done in reverse order, we actually edit the string.
Header file
#ifndef __QUESTION_1_4_H_CHONG__
#define __QUESTION_1_4_H_CHONG__
class Question1_4 {
public:
void replaceSpaces(char str[], int length);
int run();
};
#endif // __QUESTION_1_4_H_CHONG__
Source file
#include "stdafx.h"
#include <iostream>
#include "Question1_4.h"
using namespace std;
void Question1_4::replaceSpaces(char str[], int length)
{
int numOfSpaces = 0;
for (int i=0; i<length; i++) {
if (str[i]==' ') {
numOfSpaces++;
}
}
int trueLength = length + 2*numOfSpaces;
for (int i=length-1, j=trueLength-1; i>=0; i--, j--) {
if (str[i]!=' ') {
str[j] = str[i];
}
else {
str[j-2] = '%';
str[j-1] = '2';
str[j] = '0';
j = j - 2;
}
}
}
int Question1_4::run()
{
char str[100] = "Hello Chong!";
int length = 12;
replaceSpaces(str, length);
cout << str << endl;
return 0;
}
相关文章推荐
- Cracking The Coding Interview 3rd -- 1.3
- Cracking the Coding Interview Q1.4
- Cracking The Coding Interview 3rd -- 1.1*
- Cracking The Coding Interview 3rd -- 1.5*
- Cracking The Coding Interview 3rd -- 00
- Cracking The Coding Interview 1.4
- Cracking The Coding Interview 3rd -- 1.2
- cracking the coding interview No1.4
- Cracking the coding interview--Q20.12
- 《Cracking the Coding Interview》——第17章:普通题——题目8
- 《Cracking the Coding Interview》——第18章:难题——题目6
- Cracking The Coding Interview--Q1.1
- Cracking the coding interview--Q2.3
- Cracking the coding interview--Q3.2
- Cracking the coding interview--Q9.7
- Cracking the coding interview--Q1.1
- Cracking the coding interview--Q1.3
- Cracking the coding interview--Q12.1
- Cracking the Coding Interview Q1.2
- 《Cracking the Coding Interview》——第3章:栈和队列——题目5