Respuesta :
Answer: Complete a short story
#include<iostream>
//built in library provides basic input and output services
using namespace std;
//declarative region that provides a scope to the identifiers (names of the
//types, function, variables etc
int compare(char[]);
//User defined function which takes a character array as argument.
int main()
//Main body of the program
{
// 3 strings are initialized as empty: userinput, integer, word
char integer[50] = { 0 }, userinput[50] = { 0 }, word[50] = { 0 };
//Variable declaration
int value;
int loopvalue = 0;
do {
/*input string from user*/
cout << "enter string";
cin.getline(userinput, 50);
/*initialize loop variables*/
loopvalue = compare(userinput);
if (!loopvalue) {
return 0;
}
int i = 0; // for userinput
int j = 0; // after having space to store integer
int fillnum = 0; // to store integer
while (userinput[i]) {
if (userinput[i] == ' ')
{
//checking if there is a space then time to store an integer (here we are treating integer as a string)
int j = i + 1;
while (userinput[j]) {
integer[fillnum] = userinput[j];
fillnum++; j++;
}
break;
}
else {
// if there is no occurrence of space then store alphabet considering it for a word
word[j] = userinput[i];
j++;
}
i++;
}
cout << "Eating " << integer<< " "<<word <<" a day keeps the doctor away" <<endl;
} while (loopvalue);
}
int compare(char str[])
{
int i = 0;
char checkstring[] = "quit";
while (str[i] != '\0' || checkstring[i] != '\0')
// checking until the character in both arrays not reach to the null character
{
if ((str[i])>(checkstring[i]) || (str[i])<(checkstring[i]))
return 1;
else
i++;
}
return 0;
}
Explanation:
A c++ program is written to solve this problem. The explanation of each step is written in the program by using c++ comments.
// This symbol shows single line comment
/* */ These symbols shows multi line comment. Starting symbol is /* and ending symbol is */
cin.getline() is used to read unformatted string (set of characters) from the standard input device (keyboard).
char[ ] creates a character array which is like any other array.
return 0; The main function is of type int so it should return an integer value. It represent "Exit Status" of the program. Returning 0 is also a success status like saying "The program worked fine"
'\0' is the null character which terminate the string. Without it, the computer has no way to know how long that group of characters.
|| means OR. so it is true if at least one of the terms is true, false otherwise.
To see output, by giving your own input you can use any c++ compiler or IDE(Integrated Development Environment).
Happy Coding :)
![Ver imagen nooray](https://us-static.z-dn.net/files/dfb/eccfb0a2ad89eb79292ee742aa0808c2.jpg)