I dn’ot gvie a dman for a man taht can olny sepll a wrod one way. (Mrak Taiwn)
Write a function string scramble(string word) that constructs a scrambled version of a
given word, ran domly flipping two characters other than the first and last one. Then
write a program that reads words from cin and prints the scrambled words.
According to above instruction I create the following program but it is not working fine. I don't understand why this is happening. On hand tracing method, it is working properly and fine. here is my code!
#include <iostream>
#include <string>
#include <time.h>
#include <cstdlib>
using namespace std;
int main()
{
string str;
string str1;
string str2;
string scrambled_string = "";
srand(time(0));
cout << "Enter a sentence: ";
getline(cin , str);
int n = str.length();
for (int i = 0; i < n; i++)
{
do
{
str1 = str.substr(i , 1);
i++;
}
while (str.substr(i , 1) != " ");
cout << "str1: " << str1;
int j = str.length();
if (j > 3)
{
int x , y;
do
{
x = rand() % j + 1;
y = rand() % j + 1;
}
while (x != y - 1 || x == j);
string first = str1.substr(0 , x);
string last = str.substr(y + 1, j - y - 1);
string middle = str1.substr(x + 1 , y - x - 1);
str2 = first + str1.substr(y , 1) + middle + str1.substr(x , 1) + last;
}
else
{
str2 = str1;
}
scrambled_string = scrambled_string + str2;
}
cout << scrambled_string;
return 0;
}
Help me to get rid from this situation. I am using Code::Block and when I am build and run this code i get the following line after entering the sentence...!!
This application has requested to Run-time to terminate it in an unusual way. Please contact the application's support team for more information
Is there any problem with my code or I really contact to application's support team....!!

New Topic/Question
Reply



MultiQuote




|