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

could you write description for each program. #include <iostream> using namespac

ID: 3915328 • Letter: C

Question

could you write description for each program.

#include <iostream>
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., '') as the last element.
char *fname = "John";
for(int i=0; fname[i]!=''; 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 ''
char lname[] = {'D', 'o', 'e', '' };
for(int i=0; lname[i]!=''; 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 <iostream>
#include <string> // string
#include <ctime> // time()
#include <cstdlib> // rand(), srand()
using namespace std;
int main()
{
string name = "hello";
cout << "name says: " << name << endl;
return 0;
}

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

#include <iostream>

#include <string> // string

#include <ctime> // time()

#include <cstdlib> // 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 <iostream>

#include <string> // string

#include <ctime> // time()

#include <cstdlib> // rand(), srand()

using namespace std;

int main()

{

const int NWORDS = 3;

string words[NWORDS] =

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

for(int i=0; i<NWORDS; i++)

cout << words[i] << endl;

// seed random number generator

srand( static_cast<unsigned int>(time(NULL)) );

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

string wordPicked = words[r];

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

return 0;

}

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

#include <iostream>

#include <string> // string

#include <ctime> // time()

#include <cstdlib> // rand(), srand()

using namespace std;

int main()

{

const int NWORDS = 3;

string words[NWORDS] =

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

for(int i=0; i<NWORDS; i++)

cout << words[i] << endl;

// seed random number generator

srand( static_cast<unsigned int>(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<N; 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 <iostream>

#include <string> // string

#include <ctime> // time()

#include <cstdlib> // rand(), srand()

using namespace std;

int main()

{

const int NWORDS = 3;

string words[NWORDS] =

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

for(int i=0; i<NWORDS; i++)

cout << words[i] << endl;

// seed random number generator

srand( static_cast<unsigned int>(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<N; 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 <iostream>

#include <cstring> // also see <string.h> for C

using namespace std;

int main()

{

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

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

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/

Explanation / Answer

1) This program showcase usage of strings in c/c++ using char array and char pointer. Letters of first name "fname" are printed using char pointer and letters of last name are printed using char array. In the end both lname and fname printed as strings.

2) Outputs sting "name says: hello" on the console

3) This program shows how to append one string to another string using both '+' and "append()". First program prints "name says: hello" on the console. Then 'world' is appended after hello using append() and the whole string is printed "hello world". Then using '+', "is good" is also appended and printed to console as "hello world is good".

4) Program creates an array of 3 strings and displays each string one by one. Then it generates a random number between 0-2 and print string from the array words[] at the index generated by random number.

5) This program does the same as above program. After printing string at random index, it calculates the lenght of that string. Then it randomizes the letters of the string 4 times using outer loop. Inner loop picks a random index and swaps char at that index with the char ith index. At last print the randomized string.

6) This program is same as above, in addition to that is asks user to guess a string, if the user input is same as randomized string generated above then it prints "you win". It keeps on asking user to guess the word until user enters 'quit'.

7) This programs creates a sentence from the words inserted by user through console. It asks user to enter a word and concatenates the word to the string 'sentence' until '.' full stop is entered. At last print the string.