Question
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
=================================
#include
=================================
#include
#include
#include
#include
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
#include
#include
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 int r = (rand()%NWORDS); // 0,1,2 string wordPicked = words[r]; cout << "word picked is:" << wordPicked << endl; return 0; } ======================================== #include #include #include #include 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 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 #include #include 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 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 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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started