Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Write a function substr() that will scan a string for the occurrence of a give substring. the prototype of the function would be:
char *substr(char *string1,char string2);
The function should return a pointer to the element in string1 where strig2 begins. If string2 doesn’t occur in string1 then substr() should return a NULL.
For example, if string1 is “Ena Meena Deeka”, and string2 is “Meena” the substr() should return address of ‘M’ in string1.
Write main() function also that uses the function substr().
#include <iostream.h>#include<stdio.h>char *get_substr(char *sub, char *str);int main(){ char *substr,*str; cout<<"Enter the string :"; gets(str); cout<<"\nEnter the substring :"; gets(substr); substr = get_substr(substr,str); if (substr!='\0') cout << "substring found:Read more
#include <iostream.h>
#include<stdio.h>
char *get_substr(char *sub, char *str);
int main()
{
char *substr,*str;
cout<<“Enter the string :”;
gets(str);
cout<<“\nEnter the substring :”;
gets(substr);
substr = get_substr(substr,str);
if (substr!=’\0′)
cout << “substring found: ” <<substr;
else
cout<< “substring not found”;
return 0;
}
char *get_substr(char *sub, char *str) // Return pointer to substring or
null if not found.
{
int t;
char *p, *p2, *start;
for(t=0; str[t]!=’\0′; t++)
{
p = &str[t];
start = p;
p2 = sub;
while(*p2!=’\0′ && *p2==*p) // check for substring
{
p++;
p2++;
}
/* If at end of p2 (i.e., substring), then a match has been found. */ if(!*p2)
return start; // return pointer to beginning of substring
}
return 0;
}
See lessWhat is the advantage of passing arguments by reference? When and why is passing an object by reference preferable?
In passing arguments by reference method, the called function does not create its own copy of original values; rather, it refers to the original values only by different names i.e., the references. Thus the called function works with the original data and any change in the values gets reflected to tRead more
In passing arguments by reference method, the called function does not create its own copy of original values; rather, it refers to the original values only by different names i.e., the references. Thus the called function works with the original data and any change in the values gets reflected to the data. The passing an object by reference method is preferable in situations where we want the called function to work with the original object so that there is no need to create and destroy the copy of it.
See lessWrite a function having this prototype:
int replace(char *s,char c1,char c2); Have the function replace every occurrence of c1 in the string s with c2, ad have the function return the number of replacements it makes.
int replace(char * str, char c1, char c2){int count = 0;while (*str) // while not at end of string{ if (*str == c1){ *str = c2;count++;} str++; // advance to next character}return count;}
int replace(char * str, char c1, char c2)
{
int count = 0;
while (*str) // while not at end of string
{
if (*str == c1)
{
*str = c2;
count++;
} str++; // advance to next character
}
return count;
}
See lessGiven here is a structure defilation:
struct Box { char maker[21];
float height,width,length,volume;
};
Write a function that passes the address of a Box structure and that sets the volume member to the product of the other three dimensions.
#include<iostream.h>struct box{char maker[40];float height;float width;float length;float volume;};box yours;float boxer_dimensions (const box * compute );void display_box (const box make);int main(){box yours ={"Apple", 10.0, 5.0,7.5};// to initialize a recordchar name[40];float dia;dia= boxeRead more
#include<iostream.h>
struct box
{
char maker[40];
float height;
float width;
float length;
float volume;
};
box yours;
float boxer_dimensions (const box * compute );
void display_box (const box make);
int main()
{
box yours ={“Apple”, 10.0, 5.0,7.5};// to initialize a record
char name[40];
float dia;
dia= boxer_dimensions (&yours); //function call
yours.volume = dia;// assigning results to volume member record
display_box (yours);// function call
return 0;
}
float boxer_dimensions (const box *compute )// specs calls for passing address of the structure
{
float Vol;
Vol= ((compute->height)*(compute->length)*(compute->width));
//computation for volume here
return Vol;
}
void display_box (const box make)/// display all results here
{
cout << make.maker <<endl;
cout << make.height <<endl;
cout << make.width <<endl;
cout << make.length<< endl;
cout << make.volume<<endl;
}
See lessC++ lets you pass a structure by value and it lets you pass the address of a structure. If sample is a structure variable, how would you pass it by value? How would you pass its address? Which method do you think is better?
If we assume structure variable name as sample and function name as sample_function() then:Pass a structure by valuePass a structure by address struct SAMPLE sample; sample_function(sample);void sample_function(struct SAMPLE sample) : :}struct SAMPLE sample;sample_function(&sample);void sample_Read more
If we assume structure variable name as sample and function name as sample_function() then:
void sample_function(struct SAMPLE sample)
:
:
}
sample_function(&sample);
void sample_function(struct SAMPLE *sample)
{
:
:
}
Both the method have their own importance, so as per the program requirement we can use any of them.
See lessImagine a publishing company that markets both books and audio-cassette versions of its works. Create a class called Publication that stores the title (a string) and price of a publication. From this class derive two classes: Book, which adds a page count (type int); and Tape, which adds a playing time in minutes (type float). Each of the three class should have a getdata() function to get its data from the user at the keyboard, and a putdata() function to display the data. Write a main() program that creates an array of pointers to Publication. In a loop, ask the user for data about a particular book or Tape, and use new to create a object of type Book or Tape to hold the data. Put the pointer to the object in the data for all books and tapes, display the resulting data for all the books and taps entered, using a for loop and a single statement such as
pubarr[i]->putdata();
to display the data from each object in the array.
#include<iostream.h>#include<string.h>class Publication{private: char title[20]; float price;public: void getName() {cout<<"Enter Title: "; cin>>title;cout<<"Enter Price: $"; cin>>price; } void putName() {cout<<"\nTitle: "<<title;cout<<", Price:Read more
#include<iostream.h>
#include<string.h>
class Publication
{
private:
char title[20];
float price;
public:
void getName()
{
cout<<“Enter Title: “; cin>>title;
cout<<“Enter Price: $”; cin>>price;
}
void putName()
{
cout<<“\nTitle: “<<title;
cout<<“, Price: $”<<price;
}
virtual void getData() = 0;
};
class Book : public Publication
{
private:
int pages;
public:
void getData()
{
Publication::getName();
cout<<“Enter Pages: “; cin>>pages;
}
void putData()
{
Publication::putName();
cout<<“, Pages: “<<pages<<end1;
}
};
class Tape : public Publication
{
private:
float minutes;
public:
void getData()
{
Publication::getName();
cout<<“Enter Minutes: “; cin>>minutes;
}
void putData()
{
Publication::putName();
cout<<“, Minutes: “<<minutes<<end1;
}
};
int main()
{
Publication* ptrPub[100];
int n = 0;
char choice;
do
{
cout<<“Book or Tape? (b/t): “; cin>>choice;
if(choice == ‘b’)
{ ptrPub[n] = new Book; ptrPub[n]->getData(); }
else
{ ptrPub[n] = new Tape; ptrPub[n]->getData(); }
n++; cout<<“Enter another? (y/n): “; cin>>choice;
} while(choice == ‘y’);
for(int i=0; i<n; i++)
ptrPub[i]->putName();
cout<<end1;
return 0;
}
See lessSuppose 7 names are stored in a array of pointers names[] as shown below:
char *names [ ]= { “Anand”,”Naureen”,”Banjot”,”Wahid”,”sheena” };
Write a program to reverse the order of these names.
#include<iostream.h>#include<conio.h>#include<string.h>void main(){clrscr();char *name[]={"anand","naureen","banjot","wahid","sheena"};int i,j;cout<<"\nOriginal string\n";for(i=0;i<5;i++)cout<<name[i]<<end1;char *t;for(i=0,j=4;i<5/2;i++,j--){t=name[i];name[iRead more
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char *name[]={“anand”,”naureen”,”banjot”,”wahid”,”sheena”};
int i,j;
cout<<“\nOriginal string\n”;
for(i=0;i<5;i++)
cout<<name[i]<<end1;
char *t;
for(i=0,j=4;i<5/2;i++,j–)
{
t=name[i];
name[i]=name[j];
name[j]=t;
}
cout<<“\nReversed string:\n”;
for(i=0;i<5;i++)
cout<<name[i]<<end1;
getch();
}
See less