Question

In: Computer Science

1.   What is the output of the following code: string s1 = “X”; string s2 =...

1.   What is the output of the following code:
string s1 = “X”;
string s2 = “A” + s1 + “BC” + s1 + “DEF” + s1 + “G”;
cout << s2;

2.   What is the output of the following code:

string s1 = “X”;
string s2 = “A” + s1 + “BC” + s1 + “DEF” + s1 + “G”;
cout << s[0] + s[3];

3.   What is the output of the following code:
string s1 = “X”;
string s2 = “A” + s1 + “BC” + s1 + “DEF” + s1 + “G”;
cout << “Length is : “ << s.length());

4.   What is the output of the following code:
string s1 = “ABC”;
string s2 = “DEF”;
string s3;

s3 = s1 + “ “ + s2;


cout << s3;

Solutions

Expert Solution

1) Answer: AXBCXDEFXG

Explanation:

Program:

Explanation:

Line 1: //Header file
Line 2: //declarative region
Line 3: //Start of main()
Line 4: // Here Initializing the string variable called s1
Line 5:// Here Initializing the string variable called s2
Line 6:// // Here Printing the Value of s2
Line 7://End of main()

Program:

#include <iostream>
using namespace std;
int main() {
string s1 = "X";
string s2 = "A" + s1 + "BC" + s1 + "DEF" + s1 + "G";
cout << s2;
}

Output:

2) Answer: 132

Explanation:

Program:

Explanation:

Line 1: //Header file
Line 2: //declarative region
Line 3: //Start of main()
Line 4: // Here Initializing the string variable called s1
Line 5:// Here Initializing the string variable called s2
Line 6://Here declaring the string variable called s3
Line 7://Here calculating the s2[0] value s2[3] value and adding these two and Printin the result
Line 8://End of main()

Program:

#include <iostream>
using namespace std;
int main() {
string s1 = "X";
string s2 = "A" + s1 + "BC" + s1 + "DEF" + s1 + "G";
cout << s2[0] + s2[3];
}

Output:

3) Answer: Length is : 10

Explanation:

Program:

Explanation:

Line 1: //Header file
Line 2: //declarative region
Line 3: //Start of main()
Line 4: // Here Initializing the string variable called s1
Line 5:// Here Initializing the string variable called s2
Line 6:// // Here Printing the Length of s2
Line 7://End of main()

Program:

#include <iostream>
using namespace std;
int main() {
string s1 = "X";
string s2 = "A" + s1 + "BC" + s1 + "DEF" + s1 + "G";
cout << "Length is : " << s2.length();
}

Output:

4)Answer:   ABC DEF

Explanation:

Program:

Explanation:

Line 1: //Header file
Line 2: //declarative region
Line 3: //Start of main()
Line 4: // Here Initializing the string variable called s1
Line 5:// Here Initializing the string variable called s2
Line 6://Here declaring the string variable called s3
Line 7://Here calculating the s3 value by adding the two strings seperated by Space
Line 8:// Here Printing the Length of s3
Line 9://End of main()

Program:

#include <iostream>
using namespace std;
int main() {
string s1 = "ABC";
string s2 = "DEF";
string s3;
s3 = s1 + " " + s2;
cout << s3;
}

Output:


Related Solutions

True or False) The following code will output "I was true". bool isGreater(string s1, string s2)...
True or False) The following code will output "I was true". bool isGreater(string s1, string s2) { if(s1 > s2) { return true; } return false; } int main() { if(isGreater("before","back")) { cout << "I was true"; } else { cout << "I was false"; } return 0; }
Write the method: public static String removeSubstring(String s1, String s2) Method removeSubstring(String s1, String s2) Must...
Write the method: public static String removeSubstring(String s1, String s2) Method removeSubstring(String s1, String s2) Must do 3 things: 1. Take two parameters, the original String (String s1) and the substring (String s2) that is to be removed; 2. Create a new String that consists of the original String data less all occurrences of the substring data; 3. Return the new String. Note: 1. The data to be removed is each occurrence of the complete substring, not individual characters of...
Code in c# Write a recursive method called isReverse(String s1, String s2) that accepts two strings...
Code in c# Write a recursive method called isReverse(String s1, String s2) that accepts two strings as parameters and returns true if the two strings contain the same sequence of characters as each other but in the opposite order and false otherwise. • The recursive function should ignore capitalization. (For example, the call of isReverse("hello", "eLLoH") would return true.) • The empty string, as well as any one letter string, should be its own reverse. Write a driver program that...
(10 marks) Write a function to check whether string s1 is a substring of string s2....
Write a function to check whether string s1 is a substring of string s2. The function returns the first index in s2 if there is a match. Otherwise, return -1. For example, the function should return 2 if s1 is "to" and s2 is "October". If s1 is "andy" and s2 is "candy", then the function should return 1. The function prototype is as follows: int indexOf(const char *s1, const char *s2).
In C: Find a string within a string Given two strings S1 & S2, search for...
In C: Find a string within a string Given two strings S1 & S2, search for an occurrence of the second string within a first string. Note: Do not use system library for the implementation. Input: Code Zinger University Zinger where, First line represents string S1. Second line represents string S2. Output: 5 Here 'Zinger' word starts at 5th index within 'Code Zinger University’. Assume that, The length of strings S1 & S2 are within the range [1 to 10000]....
Given the strings s1 and s2 that are of the same length, create a new string...
Given the strings s1 and s2 that are of the same length, create a new string s3 consisting of the last character of s1 followed by the last character of s2, followed by the second to last character of s1, followed by the second to last character of s2, and so on
What is the output of the following code: x = 0 a = 1 b =...
What is the output of the following code: x = 0 a = 1 b = -3 if a > 0:    if b < 0:       x = x + 5    elif a > 5:       x = x + 4    else:       x = x + 3 else:     x = x + 2 print(x) 4 2 5 3 Consider the following code segment:    sum = 0.0    while True:       number = input(“Enter a...
Java RECURSIVE methods: => intersection(String s1, String s2): takes two strings and returns the string consisting...
Java RECURSIVE methods: => intersection(String s1, String s2): takes two strings and returns the string consisting of all letters that appear in both s1 and s2. => union(String s1, String s2): takes two strings and returns the string consisting of all letters that appear in either s1 or s2. =>difference(String s1, String s2): takes two strings and returns the string consisting of all letters that appear only in s1.
If there are two energy states, S1 and S2 respectively such that S2>S1 then there exists...
If there are two energy states, S1 and S2 respectively such that S2>S1 then there exists some probability for an atom in S2 to decay to S1. What actually causes the atom to decay to the lower energy state? is it the fact that the lower state is more probable for the atom to be in as given by the Boltzmann Factor? so since it is more probable, it has more microstates and entropy causes it to decay? Please help...
Answer the following Discrete Structures Suppose string s = (s1 s2 ..sn). Give a recursive definition...
Answer the following Discrete Structures Suppose string s = (s1 s2 ..sn). Give a recursive definition of the function numOnes(n), which counts the number of 1s in bit-string of length n, Make sure to define the function for the base case, numOnes(0).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT