The idea is to get a sentence of words and then scramble the middle letters of each word without altering the position of the word in the sentance. So for example, the user inputs: Programming is very interesting! the output should be: Pogrmarmnig is vrey itenrseitng! So far, i have been able to write a program that scrambles the letters of only 1 word and would like to see if someone would be friendly enough to help me get an idea of what i can do to modify a string of words. Here is my code:

#include <iostream>
#include <cstring>

using namespace std;

string switcharoo(string s){
int frst = 1;
int lst = s.size()-1;

for (int i = frst; i < lst; i++){
srand (time(NULL));
int j= 1+rand() % lst;

char t = s;

s = s[j];

s[j] = t;

return s;

int main(){
string stmt;
cout << "type a sentance: ";
getline(cin, stmt);
stmt = switcharoo(stmt);
cout << stmt;

return 0;

5 Years
Discussion Span
Last Post by iamthwee

Firstly please use code tags, it makes your code easier to read.

For your problem you really have the first part done, try making a second function that will parse words from your sentance using the space delimiter. And for each word decide what to do with it. as each word is processed append it to a result string and return this result string to your final stmt. So something like this as a sekelton

string ParseSentance( string& sentance )
   string word;
   string resultSentance;

/* make a loop here to do this for each word in the sentance */

   /* get a word from the string */
   word = GetOneWordFromSentance( sentance ); /* you will have to write this function */

   if( true = someCondition  )
     word = DoLetterScramble( word );

   /* append to the result */

/* the for each word loop would end here */

   return sentanceString;

Something along those lines is probibally what you are looking for.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.