Wednesday, 4 December 2019

Write a program that has a declaration in main() to store the string Vacation

Q: Write a program that has a declaration in main() to store the string Vacation is near in an array named message. Include a function call to display () that accepts message in an argument named string and then displays the contents of message by using the pointer notation *(strng + i).
Then modify the display() function written to use the expression *strng rather than *(strng +i) to retrieve the correct element.


Solution:

#include <iostream>

using namespace std;

void display(char* string)
{
for(int i=0; i<strlen(string); i++)
cout << *(string+i);
}

void display2(char* string)
{
int len = strlen(string);
for(int i=0; i<len; i++)
{
cout << *string;
string++;
}
}

int main()
{
char message[50] = "Vacation is near";


display(message);
cout << endl;
display2(message);
return 0;

No comments:

Post a Comment