-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1.4.cpp
More file actions
70 lines (58 loc) · 1.41 KB
/
1.4.cpp
File metadata and controls
70 lines (58 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Nicole Kulakowski
// Question 1.4
// 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.)
#include <iostream>
#include <string>
using namespace std;
int numberOfCharsInArray(char* array) {
int numberOfChars = 0;
//cout<<array.length<<endl;
int i = 0;
while(array[i] != '\0') {
numberOfChars++;
i++;
}
return numberOfChars;
}
void change(char* input){
int spaceCount = 0, newLength, oldLength;
oldLength = numberOfCharsInArray(input);
for (int i = 0; i < oldLength; ++i)
{
if (input[i] == ' ')
{
spaceCount++;
}
}
newLength = oldLength + spaceCount*2;
newLength+=1;
input[newLength] = '\0';
for (int i = oldLength; i >= 0; i--)
{
if (input[i] == ' ')
{
input[newLength-1] = '0';
input[newLength-2] = '2';
input[newLength-3] = '%';
newLength = newLength - 3;
}
else
{
input[newLength-1] = input[i];
newLength = newLength-1;
}
cout<<input<<endl;
}
}
int main(int argc, char const *argv[])
{
char str[] = "Hello my name is Nicole.\0";
cout<<"before: "<<str<<endl;
change(str);
cout<<"after: "<<str<<endl;
return 0;
}