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
cout
statement prints the modified stringstr
to the console.
So, the output is "h123ello".
Similar Questions
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 code snippet? String str1 = "Java"; String str2 = new String("Java"); System.out.println(str1 == str2);
What is the output of the following code?char str[] = "hello";int len = strlen(str);printf("%d\n", len);
What will be print after executing following statement int value=65; char ch=value; cout<
What is the output of the following C code?char *ptr;char mystring[] = "abcdefg";ptr = myString;ptr += 5;
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.