What is the output of the following code?string str = "hello";str.insert(1, "123");cout << str;"h123ello""1hello""hello123""h123lo"
Question
What is the output of the following code?string str = "hello";str.insert(1, "123");cout << str;"h123ello""1hello""hello123""h123lo"
Solution
The output of the code will be "h123ello".
Here's the step by step explanation:
- A string "hello" is created and assigned to the variable
str. - The
insert()function is called onstr. This function inserts the string "123" at the position 1 ofstr. The position of the characters in a string is zero-based, which means the first character is at position 0, the second character is at position 1, and so on. So, "123" is inserted after the first character 'h'. - The
coutstatement prints the modified stringstrto the console.
So, the output is "h123ello".
Similar Questions
What is the output of the following code?string str = "Hello";cout << str.length();4560
What will be the output of the following code?string str = "Hello World";str.replace(6, 5, "C++");cout << str;"Hello C++""Hello C++ World""C++ World""World C++"
What will be the output of the following code?string str = "abcdef";cout << str.substr(2, 3);"abc""def""cde""bcd"
What is the output of the following snippet? #include <iostream> #include <string> using namespace std; int main() { string s1[]= {"A","Z"}; string s=""; for (int i=0; i<2; i++) cout << s.append(s1[i]).insert(1,"_"); return( 0 ); } A_A__Z A__Z A_A_Z A_A
What is the output of the following code snippet? String str1 = "Java"; String str2 = new String("Java"); System.out.println(str1 == str2);
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.