swap different sized string
-
how can I write a function to swap to different sized string?
What do you mean exactly :confused: ? Could you provide an example describing what you are looking for ?
Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++ -
how can I write a function to swap to different sized string?
I cannot understand what is the difficulty in writing that? can you clarify your question.
Величие не Бога может быть недооценена.
-
What do you mean exactly :confused: ? Could you provide an example describing what you are looking for ?
Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++Like in main function I will read two string from user. I will store the string either in array of fixed size or in a pointer or simply say in the main() there are two strings: char *c = "MSN" char *d = "YAHOO" I have to pass these two string in a function say swapstrings() and swap these two variables
-
how can I write a function to swap to different sized string?
-
how can I write a function to swap to different sized string?
Just swap their pointers :) :
{
LPSTR lpStrA("12345");
LPSTR lpStrB("0987654321");LPSTR lpTemp(lpStrA);
lpStrA = lpStrB;
lpStrB = lpTemp;
}virtual void BeHappy() = 0;
-
I cannot understand what is the difficulty in writing that? can you clarify your question.
Величие не Бога может быть недооценена.
the issue occurs with the different sized strings
-
how can I write a function to swap to different sized string?
If you're using C++ you don't need to write a function to do it, you just use std::swap. If you're using C then you've got two choices: - Swap the pointers to the arrays of characters you're interested in. IF you can get away with this it's pretty quick. - If you've really got to move characters between arrays then they'd better both be large enough to accomodate the text in either string. You can either exchange each character over the first N characters where the number of characters is equal to the length of the longest string OR do a circular copy through a third buffer the length of the longest string. The circular copy method means you can use strcpy though. Cheers, Ash
-
Like in main function I will read two string from user. I will store the string either in array of fixed size or in a pointer or simply say in the main() there are two strings: char *c = "MSN" char *d = "YAHOO" I have to pass these two string in a function say swapstrings() and swap these two variables
You might feel this is being overly pedantic... trust me it's not. You don't store strings in pointers - all you store in pointers are addresses. It just so happens that the character pointers you're talking about point to the first character of the block of memory you've got the characters representing the string in. If you understand that then it should be fairly easy to work out how to switch the pointers around and get the result you're after. Cheers, Ash
-
If you're using C++ you don't need to write a function to do it, you just use std::swap. If you're using C then you've got two choices: - Swap the pointers to the arrays of characters you're interested in. IF you can get away with this it's pretty quick. - If you've really got to move characters between arrays then they'd better both be large enough to accomodate the text in either string. You can either exchange each character over the first N characters where the number of characters is equal to the length of the longest string OR do a circular copy through a third buffer the length of the longest string. The circular copy method means you can use strcpy though. Cheers, Ash
thanks ash for the clarification
-
inline void inventTheWheel(std::string &s1, std::string &s2)
{
std::swap(s1, s2);
}Another way is to just use the std::swap function directly.
thanks niklas