SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,julia Lobur
Chapter:
Strings And Vectors
Exercise:
Self-test Exercises
Question:2 | ISBN:9780321531346 | Edition: 7

Question

What C string will be stored in singing_string after the following code

is run?

char singing_string[20] = "DoBeDo";

strcat(singing_string, " to you");

Assume that the code is embedded in a complete and correct program and that an include directive for <cstring> is in the program file.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

The output of the given code statements:

"DoBeDo to you"

Explanation:

  • The C string will be stored in singing_string as "DoBeDo"
  • The next statement is using to concat to given above string and "to you" by using default function of strcat.  That is, DoBeDo to you
  • So, string will be stored in singing_string after the following code is executed, the result is "DoBeDo to you".

Complete executable code:

// Header file section
#include <iostream>
#include<cstring>
using namespace std;

//main program
int main()
{
    //Initialize the variable
  char singing_string[20] = "DoBeDo";
  //Concat the two strings by using strcat function
  strcat(singing_string, " to you");
  
  //Print the final result
  cout<<singing_string<<endl;
  return 0;
  
}

 

0 0

Discussions

Post the discussion to improve the above solution.