I don't know how to run this code on visual studios and I don't understand why the answer is 211111118. Please explain! Thank you!
CONSIDER THE FOLLOWING CODE:
void Question()
{
string x = "12";
mystery1(x);
string str1 = "21";
mystery2(str1);
string str2 = "11";
mystery2(str2);
string str3 = "31";
mystery2(str3);
string str4 = "91";
mystery2(str4);
string str5 = "81";
mystery1(str5);
x = x + str1 + str2 + str3 + str4 +
str5;
cout << x;
}
////////////////////////////////////////////////////////////
void mystery1(string &a)
{
int num = a.length();
string temp;
for (int i = 0 ; i < num ;
i++)
{
temp
= a.at(i) + temp;
}
a = temp;
}
////////////////////////////////////////////////////////////
void mystery2(string &b)
{
int num = b.length();
string temp ="";
for (int i = 0; i < num;
i++)
{
if(b.at(i) == '1')
temp
= temp + b.at(i);
}
b = temp;
}
FULL CODE
#include "stdafx.h"
#include <iostream>
using namespace std;
////////////////////////////////////////////////////////////
void mystery1(string &a)
{
int num = a.length();
string temp;
for (int i = 0 ; i < num ; i++)
{
temp = a.at(i) + temp;
}
a = temp;
}
////////////////////////////////////////////////////////////
void mystery2(string &b)
{
int num = b.length();
string temp ="";
for (int i = 0; i < num; i++)
{
if(b.at(i) == '1')
temp = temp + b.at(i);
}
b = temp;
}
void Question()
{
string x = "12";
mystery1(x);
string str1 = "21";
mystery2(str1);
string str2 = "11";
mystery2(str2);
string str3 = "31";
mystery2(str3);
string str4 = "91";
mystery2(str4);
string str5 = "81";
mystery1(str5);
x = x + str1 + str2 + str3 + str4 + str5;
cout << x;
}
int main() {
Question();
}
Explanation:
mystery1() function modifies the string passed to it as the argument to its reverse.
mystery2() function modifies the string passed to it as the argument such that it contains only 1's now.
Hence,
string x = "12";
mystery1(x); ------> x = "21"
string str1 = "21";
mystery2(str1); ------> str1= "1"
string str2 = "11";
mystery2(str2); ----> str2 = "11"
string str3 = "31";
mystery2(str3); ------> str3 = "1"
string str4 = "91";
mystery2(str4); -----> str4 = "1"
string str5 = "81";
mystery1(str5); ------> str5 = "18"
x = x + str1 + str2 + str3 + str4 + str5; -----> x = "21" + "1" + "11" + "1" + "1" + "18" = "211111118"
cout << x;
I don't know how to run this code on visual studios and I don't understand why...
SCREENSHOTS ONLY PLEASE!!! DON'T POST ACTUAL
CODE
PLEASE LEAVE A SCREENSHOT ONLY! ACTUAL TEXT IS NOT
NEEDED!!!
mystring.h:
//File: mystring1.h
// ================
// Interface file for user-defined String class.
#ifndef _MYSTRING_H
#define _MYSTRING_H
#include<iostream>
#include <cstring> // for strlen(), etc.
using namespace std;
#define MAX_STR_LENGTH 200
class String {
public:
String();
String(const char s[]); // a conversion constructor
void append(const String &str);
// Relational operators
bool operator ==(const String &str) const;
bool operator !=(const String &str) const;
bool operator >(const...
Using C++ Use the below program. Fill in the code for the 2 functions. The expected output should be: The:fox:jumps:over:the:fence.: The:fox:jumps:over:the:fence.: The:fox:jumps:over:the:fence.: The:fox:jumps:over:the:fence.: #include #include #include using namespace std; //Assume a line is less than 256 //Prints the characters in the string str1 from beginIndex //and inclusing endIndex void printSubString( const char* str1 , int beginIndex, int endIndex ) { } void printWordsInAString( const char* str1 ) { } int main() { char str1[] = "The fox jumps over the...
Problem with C++ program. Visual Studio say when I try to debug "Run-Time Check Failure #2 - Stack around the variable 'string4b' was corrupted. I need some help because if the arrays for string4a and string4b have different sizes. Also if I put different sizes in string3a and string4b it will say that those arrays for 3a and 3b are corrupted. But for the assigment I need to put arrays of different sizes so that they can do their work...
Language is in C. Need help troubleshooting my code Goal of code: * Replace strings with a new string. * Append an s to the end of your input string if it's not a keyword. * Insert a period at the end, if you have an empty input string do not insert a period Problems: //Why does my code stop at only 2 input strings //If I insert a character my empty string case works but it's still bugged where...
when i run it is still be "false" i don't know why. could you
help me fix it and tell me which part is wrong??? specific answer
is better thanks!!!
Problem 6: (4 pts): Write a function that takes as an input parameter a string. It should return a Boolean value indicating whether the string is a palindrome or not. INPUT: “mom"->true “palindrome”->false “amanaplanpanama"->true 3 #include <iostream> #include <string.h> I using namespace std; string function(string str); int main() { function("palindrome");...
I'm writing a simple High-Low Game for my Fundamentals of Programming I class but in Visual Studios it keeps saying that my two different functions are undeclared I don't know where the problem is could you help? This is what I have so far: #include<iostream> #include<cstdlib> #include<ctime> #include<string> using namespace std; int main(); int rndmNum(); int rndmNum() { int num, n; srand(time(0)); num = rand() % 100 + 1; return num; } int userGuess(int num) {...
Ship, CruiseShip, and CargoShip Classes (in C++ language i use visual studios to code with) design a Ship class that has the following members: - A member variable for the name of the ship (a string) - A member variable for the year that the ship was built (a string) - A contsructor and appropriate accessors and mutators - A virtual print function that displays the ship's name and the year it was built (nobody seems to get this part...
CODE ERROR Please help to fix the error. I don't know why I can not output to file. when I open the output file there is an error. here is the input data: Princeton University NJ Princeton 41820 8014 0.0740 0.98 0.97 Harvard University MA Cambridge 43838 19882 0.0580 0.97 0.97 Yale University CT New Haven 45800 12109 0.0690 0.99 0.98 Columbia University NY New York 51008 23606 0.0690 0.99 0.96 Stanford University CA...
CONSIDER THE FOLLOWING CODE: double SumString(string x, int n) { double sum = 0; for (int i = 0; i < n; i += 2) sum += x.length(); return sum; } ///////////////////////////////////////////////////////// void Question() { string a = "string"; int num = a.length(); double total = SumString(a, num); cout << "The total is: " << total << endl; } Select one: a. The total is: 14.0 b. The total is: 20.0...