is there a way to swap character places in a string? For example if I have "03/02"
I need to get "02/03"
.
Any help is appreciated!
For swapping two strings from one location to another location, we use strcpy() function. An array of characters (or) collection of characters is called a string.
As we know that Object of String in Java are immutable (i.e. we cannot perform any changes once its created). To do modifications on string stored in a String object, we copy it to a character array, StringBuffer, etc and do modifications on the copy object.
str_swap(&char_array[0],&char_array[2]); shouldn't compile, because &char_array[0] and &char_array[2] are not char** s. Swapping char[] is not the same as swapping pointers. If you want to swap pointers, your array needs to contain pointers, not arrays (and your swap function is close to correct, btw).
Sure:
#include <string>
#include <algorithm>
std::string s = "03/02";
std::swap(s[1], s[4]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With