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;
}

Recommended Answers

All 4 Replies

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 */
   resultSentance+=word;

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

   return sentanceString;
}

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

Thanks. could you help me setup a loop that would work for this?

for (i=second_letter; i<second_to_last_letter; i++)

Member Avatar for iamthwee
i<second_to_last_letter

Can anyone else see the error in this logic ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.