Question
According to a Cambridge University study, most people can read words in sentences with scrambled letters, provided the first and last letters of words are
According to a Cambridge University study, most people can read words in sentences with scrambled letters, provided the first and last letters of words are the same. For example: tihs setnnece is srcmalbed, colud you udnretsnad it?
(It said: this sentence is scrambled, could you understand it?)
You are curious to try this out yourself, so you decide to write a program that processes sentences as strings, scrambles them, and prints out the result. For this problem, we will focus on your string scrambling function, which will handle one word at a time. Rather than randomly moving letters around, you decide on a fixed set of rules.
Objective
Implement the function:
string scrambleWord(string word)
Apply the following rules:
-
- Swap letters at odd indices with the letter one index before them.
-
- Remember that string indices start at 0, not 1, so the first letter has an even index, the
second letter an odd index, and so on.
-
- Do not move the first or last letter, regardless of index.
-
- In a swap, make sure that i is always given the first position; that is, do not swap if
another letter would get a lower index than i. Examples
scrambleWord("rambunctious") = rabmnutcoius scrambleWord("optimistic") = opitimtsic scrambleWord("bountiful") = bonuitufl scrambleWord("software") = sotfawre scrambleWord("quell") = qulel scrambleWord("unique") = unqiue scrambleWord("value") = vaule
Code:
#include
using std::cout, std::cin, std::endl;
int main() { int test[10] = { -12, 2, -2, 3, 5, -4, 78, -3, 19, 33}; // the largest difference between values should be 82 unsigned int answer = 0; // loop and compare for ( ) {
} return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started