Warning: Declaration of mosaic_walker_nav_menu_dropdown::start_lvl(&$output, $depth) should be compatible with Walker_Nav_Menu::start_lvl(&$output, $depth = 0, $args = Array) in /home/linkevin/public_html/wp-content/themes/mosaic/functions.php on line 537

Warning: Declaration of mosaic_walker_nav_menu_dropdown::end_lvl(&$output, $depth) should be compatible with Walker_Nav_Menu::end_lvl(&$output, $depth = 0, $args = Array) in /home/linkevin/public_html/wp-content/themes/mosaic/functions.php on line 537

Warning: Declaration of mosaic_walker_nav_menu_dropdown::start_el(&$output, $item, $depth, $args) should be compatible with Walker_Nav_Menu::start_el(&$output, $item, $depth = 0, $args = Array, $id = 0) in /home/linkevin/public_html/wp-content/themes/mosaic/functions.php on line 537

Warning: Declaration of mosaic_walker_nav_menu_dropdown::end_el(&$output, $item, $depth) should be compatible with Walker_Nav_Menu::end_el(&$output, $item, $depth = 0, $args = Array) in /home/linkevin/public_html/wp-content/themes/mosaic/functions.php on line 537

Warning: Parameter 2 to wp_hide_post_Public::query_posts_join() expected to be a reference, value given in /home/linkevin/public_html/wp-includes/class-wp-hook.php on line 286
COTD: Replacing Space with "" in a String (C++, Ex: 1.4)

Replacing Spaces with “%20” in a String (C++, Ex: 1.4)

string space to %20 cracking

There are multiple ways to replace spaces in a string with “%20”, but one way I did it without starting from the back is through string concatenations and substrings (substr).

In C++:

int main() {
	string str = "hello world this is an example.";
	string strF, strB;

	for (int i = 0; i < str.length(); i++) {
		if (str[i] == ' ') {
			strF = str.substr(0, i);
			strB = str.substr(i + 1, str.length() - (i + 1));
			str = strF + "%20";
			str = str + strB;
		}
	}
	cout << "\nSolution: \n" << str << endl;
}

Output:

hello%20world%20this%20is%20an%20example.

Substr takes in two inputs and returns a string that starts from position index and spans for length characters. In our above example, str.substr(0, i) when i = 5 ([5] == ‘ ‘) will return “hello” (starting from position 0 and spanning 5 chars).

My reasoning in this example was that I would take substrings of everything before and after the space, and then concatenate them together with a “%20” in between. Corner cases weren’t checked, but it works well in normal cases.

To check what were in the substrings, I inserted two cout statements — one after line 7 and one after line 8:

cout << "strF: " << strF << " ";
strB = str.substr(i + 1, str.length() - (i + 1));
cout << "strB: " << strB << ". " << endl;

This gave an output of:

strF: hello     strB: world this is an example..
strF: hello%20world     strB: this is an example..
strF: hello%20world%20this     strB: is an example..
strF: hello%20world%20this%20is     strB: an example..
strF: hello%20world%20this%20is%20an     strB: example.. 

Solution:
hello%20world%20this%20is%20an%20example.

 

Kevin Lin

I am a senior at Wake Forest University currently pursuing my BS in Computer Science, and I love technology! To learn a little more about me, please visit the "Meet Kevin" widget on the top right on the screen!
Posted in C++, Code.

Leave a Reply

Your email address will not be published. Required fields are marked *