Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Programming Help?C-style strings Need help on some homework. Here are the qu

ID: 3559362 • Letter: C

Question

C++ Programming Help?C-style strings

Need help on some homework. Here are the questions... I am new to C++ and this part has been giving me fits of issues... can anyone help me out with the following problems!? Would really appreciate it! Thanks!

1#Why the capacity and the length values for each string are different in the output produced by the program.(sizeof() function returns for an array of characters)

2#what does the statement "strcpy(his_name,"Barack Obama");" do?

3#Chaging the #include library from "string" to "cstring". Does the program complie without Producing any errors? What is cstring?

Investigate the declaration, initialization and implementation of C-style strings.

#include <iostream>

#include <string>

using namespace std;

int main()

{

                char my_name[20] = "James Madison";

                char her_name[ ] = "Michelle Obama";

                char his_name[20];

                cout<<"my_name = "<<my_name<<endl;

                cout<<"The length of my_name is "<<strlen(my_name)<<endl;

                cout<<"The capacity of my_name is "<<sizeof(my_name)<<endl;

                cout<<"her_name = "<<her_name<<endl;

                cout<<"The length of her_name is "<<strlen(her_name)<<endl;

                cout<<"The capacity of her_name is "<<sizeof(her_name)<<endl;

                strcpy(his_name,"Barack Obama");

                cout<<"his_name = "<<his_name<<endl;

                cout<<"The length of his_name is "<<strlen(his_name)<<endl;

                cout<<"The capacity of his_name is "<<sizeof(his_name)<<endl;

                return 0;

               

}

Explanation / Answer

1#Why the capacity and the length values for each string are different in the output produced by the program.
(sizeof() function returns for an array of characters)
size of array returns length of array. while strlen returns the length of string. because string terminated with null character.

2#what does the statement "strcpy(his_name,"Barack Obama");" do?
copies "Barack Obama to character arry his_name.
3#Chaging the #include library from "string" to "cstring". Does the program complie without Producing any errors? What is cstring?
Investigate the declaration, initialization and implementation of C-style strings.
Yes. it runs with out any compilation or run time errors. cstring is header declaration involving c style strings.
C++ style strings are different they are having declaration string str; and for copying direct assignment str = "Barack Obama";

#include <iostream>
#include <string>
using namespace std;
int main()
{
   char my_name[20] = "James Madison";
   char her_name[ ] = "Michelle Obama";
   char his_name[20];
   cout<<"my_name = "<<my_name<<endl;
   cout<<"The length of my_name is "<<strlen(my_name)<<endl;
   cout<<"The capacity of my_name is "<<sizeof(my_name)<<endl;
   cout<<"her_name = "<<her_name<<endl;
   cout<<"The length of her_name is "<<strlen(her_name)<<endl;
   cout<<"The capacity of her_name is "<<sizeof(her_name)<<endl;
   strcpy(his_name,"Barack Obama");
   cout<<"his_name = "<<his_name<<endl;
   cout<<"The length of his_name is "<<strlen(his_name)<<endl;
   cout<<"The capacity of his_name is "<<sizeof(his_name)<<endl;
   return 0;
}

/* OUTPUT
my_name = James Madison
The length of my_name is 13
The capacity of my_name is 20
her_name = Michelle Obama
The length of her_name is 14
The capacity of her_name is 15
his_name = Barack Obama
The length of his_name is 12
The capacity of his_name is 20
*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote