A spoonerism

Pages: 12
I have this exercise on my textbook but its confusing me, it says:

The program must input two words and output their spoonerism e.g Cold Blue is Bold Clue. Can someone show me how to play around spoonerism?
Swap the first letter of each word.
I think it's exchanging the first letters of the words.

When we exchange, or 'swap' two variables, often we create a temporary variable to hold one, then we assign the other to the position of the first and copy over the temp to its new location.

e.g.

1
2
3
4
5
char a = 'a', b = 'b', temp;

temp = b;
b = a;
a = temp;


I hope this is enough of an idea. Unless you don't know how to input words?
Can you please guys give me just 1 detailed example of swapping words

I was thinking that am supposed to do something like this:

string string1, string2,...;
cout<<"Enter two words"<<endl;
cin>>string1>>string2;
cout<<..........

but now how do i swap within a word such that it changes from Cold Blue is Bold Clue.



1
2
3
char temp = string1[0] ;
string1[0] = string2[0];
string2[0] = temp;


OMG it's exactly the same code as Veltas put up!
It really is as simple as I was saying earlier...

1
2
3
4
5
char temp;

temp = string1[0];
string1[0] = string2[0];
string2[0] = temp;


EDIT: My thoughts exactly, Moschops...
Last edited on
@Moschops and Veltas: thank you so much, i had no clue and its working.
Last edited on
That was my best NINNJAARR ever :) I very nearly guessed exactly the code Veltas was going to write.
That was quite amazing, but only testament to how obvious the answer was; hombakazij I hope you're very tired or something, because really you have to at least try and understand what we tell you and make use of it rather than just be spoon-fed, or you'll learn nothing from this!
Isn't there an std::swap()?
Save it for when they know how to swap.
Fair enough.
@Veltas: I am going to really try my best, am new in programming but that's not an excuse for me to be spoon-fed. And i do understand the program now i even worked out the output on a paper trying to understand it, and now i get it. thanks a lot.
Good to hear, this is the only way you can learn the skill of programming, or hell; pretty much anything.
"Knowing how to swap" is knowing how to use std::swap(). If you think about it in terms of assignments, you may think that swapping two strings or two vectors involves copying.
Last edited on
Maybe Cubbi, but maybe standard library functions like swap are beyond beginners.
I think it's easier to understand std::swap than it is to understand which variable to stick in a temp variable, order of swapping etc. I agree that beginners should do this themselves just to understand it, but I also say they should get involved in the STL as soon as possible, as opposed to the old "Let's learn the C stuff with C++"
I'm sorry but in my early C++ days I clinged to all the rules given to me and tried to work with the C++ I could in order to produce what I needed. If you had introduced specific STL functions to me I would have complained it was too much information for a beginner and said that I'd learn to use specific functions when it came to it but for now the pure basics were hard enough.

And if you had shown me swap, I wouldn't have seen it as easier to understand. For one thing it either requires you have a good idea of how functions work, or you're prepared to blindly do whatever they ask of you without understanding; which in my books isn't learning, or even helpful if there's another way that will give you more experience with the core programming functionality.
@Veltas When you don't know what you're doing and why, it's useless to learn how. Only after learning how to *use* basic C++ components - vector, swap, cout, etc, it makes any sense to start implementing your own classes and functions.
I'm not an expert in C++ but I'm going to side with Veltas on this one. Cubbi, you say that one should learn how to use basic C++ components like swap. However, I think the solution Veltas and Moschops show above is much more basic than this swap function, indeed, the swap function most likely has a similar functionality to what they did.

I believe that it is much better to first learn how to solve a problem using the very basics, and then at a later time learn the included C++ functions like swap. This way you build your problem solving skills, so in the event that you run into a problem for which you cannot think of a suitable built-in function to solve, you can do it yourself having a solid understanding of the essential basics. The built-in functions, while a valuable and efficient tool for more versed C++ programmers, should be used sparingly by beginners.

Just my two cents.
Pages: 12