Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

could you write description for each program. #include using namespace std; // notice in C/C++ *data can be the same as data[]. // int *data

could you write description for each program.

#include using namespace std; // notice in C/C++ *data can be the same as data[]. // int *data means pointer to at least one integer. // int data[] means data is the address of the beginning // of an array of at least one integer // n.b.: // if data==NULL (e.g., 0x0), then it points to nothing int main() { cout << "THE STRINGS OF THE C/C++ LANGUAGE" << endl; // The compiler builds the string inserting an ASCII // zero (i.e., '\0') as the last element. char *fname = "John"; for(int i=0; fname[i]!='\0'; i++) cout << fname[i] << endl; cout << endl; // skip a line so output looks good. // manually build a string by filling a character // array, so you must set the last element to '\0' char lname[] = {'D', 'o', 'e', '\0' }; for(int i=0; lname[i]!='\0'; i++) cout << lname[i] << endl; cout << endl; // skip a line so output looks good. // cout is programed to correctly display strings cout << lname <<','<< fname << endl; return 0; }

=================================

#include #include // string #include // time() #include // rand(), srand() using namespace std; int main() { string name = "hello"; cout << "name says: " << name << endl; return 0; }

=================================

#include

#include // string

#include // time()

#include // rand(), srand()

using namespace std;

int main()

{

string name;

name = "hello";

cout << "name says: " << name << endl;

name.append(" world");

cout << name << endl;

// + is overloaded to append

name = name + " is good";

cout << name << endl;

return 0;

}

=====================================

#include

#include // string

#include // time()

#include // rand(), srand()

using namespace std;

int main()

{

const int NWORDS = 3;

string words[NWORDS] =

{ "house", "door", "wall" };

for(int i=0; i

cout << words[i] << endl;

// seed random number generator

srand( static_cast(time(NULL)) );

int r = (rand()%NWORDS); // 0,1,2

string wordPicked = words[r];

cout << "word picked is:" << wordPicked << endl;

return 0;

}

========================================

#include

#include // string

#include // time()

#include // rand(), srand()

using namespace std;

int main()

{

const int NWORDS = 3;

string words[NWORDS] =

{ "house", "door", "wall" };

for(int i=0; i

cout << words[i] << endl;

// seed random number generator

srand( static_cast(time(NULL)) );

int r = (rand()%NWORDS); // 0,1,2

string wordPicked = words[r];

cout << "word picked is:" << wordPicked << endl;

int N = wordPicked.length();

for(int scram=0; scram<4; scram++)

for(int i=0; i

{

int index = rand()%N; // 0, 1, ..., N-1

char temp = wordPicked[i]; // instead of .at(i)

wordPicked[i] = wordPicked[index];

wordPicked[index] = temp;

}

cout << "word picked is:" << wordPicked << endl;

return 0;

}

==================================

#include

#include // string

#include // time()

#include // rand(), srand()

using namespace std;

int main()

{

const int NWORDS = 3;

string words[NWORDS] =

{ "house", "door", "wall" };

for(int i=0; i

cout << words[i] << endl;

// seed random number generator

srand( static_cast(time(NULL)) );

int r = (rand()%NWORDS); // 0,1,2

string wordPicked = words[r];

cout << "word picked is:" << wordPicked << endl;

int N = wordPicked.length();

for(int scram=0; scram<4; scram++)

for(int i=0; i

{

int index = rand()%N; // 0, 1, ..., N-1

char temp = wordPicked[i]; // instead of .at(i)

wordPicked[i] = wordPicked[index];

wordPicked[index] = temp;

}

cout << "word picked is:" << wordPicked << endl;

// could be made in to a game

string guess;

do {

cout << "guess: ";

cin >> guess;

if ( guess == wordPicked )

{

cout << " you win ";

break;

}

} while (guess != "quit");

return 0;

}

=========================================

#include

#include // also see for C

using namespace std;

int main()

{

cout << "The C String Library" << endl;

char sentence[80]; // can hold 79 + '\0'

char word[20];

strcpy(sentence, ""); // sentence="";

strcpy(word,""); // word="";

// get words and build a sentence

do {

cout << "enter a word:";

cin >> word;

strcat(sentence, " ");

strncat(sentence, word, strlen(word));

} while (strcmp(word, ".")!=0);

cout << sentence << endl;

return 0;

}

// reference:

// http://www.cplusplus.com/reference/cstring/

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design Application Development And Administration

Authors: Mannino Michael

5th Edition

0983332401, 978-0983332404

More Books

Students also viewed these Databases questions

Question

5. What are the six primary

Answered: 1 week ago