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; }
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).
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.
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...
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).
Write the following Python code: A string X is an anagram of string Y if X...
Write the following Python code: A string X is an anagram of string Y if X can be obtained by arranging all characters of Y in some order, without removing any characters and without adding new characters. For example, each of the strings "baba", "abab", "aabb" and "abba" is an anagram of "aabb", and strings "aaab", "aab" and "aabc" are not anagrams of "aabb". A set of strings is anagram-free if it contains no pair of strings which are anagrams...
Write the code to return the output in Rstudio. What is the code? Code: x <-...
Write the code to return the output in Rstudio. What is the code? Code: x <- c(28, 69, 5, 88, 19, 20) Output must be: [1] 4 2 1 6 5 3
def anagramSolution1(s1,s2): alist = list(s2) pos1 = 0 stillOK = True while pos1 < len(s1) and...
def anagramSolution1(s1,s2): alist = list(s2) pos1 = 0 stillOK = True while pos1 < len(s1) and stillOK: pos2 = 0 found = False while pos2 < len(alist) and not found: if s1[pos1] == alist[pos2]: found = True else: pos2 = pos2 + 1 if found: alist[pos2] = None else: stillOK = False pos1 = pos1 + 1 return stillOK include all operations and calculate the Big-O and the values for c and n0.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT