Need the full working code
the out put must be like this
mohit + bhalla = mohit bhalla
abcpqrabc - pqr= abcabc;
we have to use operator overloading and copy constructor;
okies
if any one have time than please hlp me
i need it
{for eg in main i have char c1,c2,c3;
c1=mohit;
c2=bhalla;
than c3=c1+c2;
c3.print();
}out put mst be mohitbhalla
mohit + bhalla = mohit bhalla
abcpqrabc - pqr= abcabc;
we have to use operator overloading and copy constructor;
okies
if any one have time than please hlp me
i need it
{for eg in main i have char c1,c2,c3;
c1=mohit;
c2=bhalla;
than c3=c1+c2;
c3.print();
}out put mst be mohitbhalla
Replies
-
yamrajbhallaany hlp plzzzz
-
shalini_goel14
Please specify in which language you need? ๐yamrajbhallaany hlp plzzzz -
Raviteja.gif you want the code in java here it is
import java.io.*;
class add
{
public static void main(String args[])
{
String c1="mohit";
String c2="bhalla";
String c3=c1+c2;
System.out.println(c3);
}
} -
yamrajbhalla#include
#include
#include
class b
{
char nm[30];
public:
b()
{strcpy(nm,"unknown");}
b(char nm[])
{
strcpy(this->nm,nm);
}
void setname(char m[])
{
strcpy(nm,m);
}
void print()
{
cout<}
b operator +(b c)
{
b t;
char nem[50];
for(int i=0;nm!='\0';i++)
{ nem=nm;
for(int j=0;c.nm[j]!='\0';j++)
{nem[i+j+1]=c.nm[j];
} }
strcpy(t.nm,nem);
return t;
}
};
void main()
{
b b1,b2,b3;
clrscr();
b1.setname("mohit");
b2.setname("bhalla");
b3=b1+b2; -
yamrajbhallab1.print();cout<<" + ";b2.print();cout<<" = ";b3.print();
getch();
}
in c++ -
yamrajbhallathe above code is not wrking good its shhowing mohitbhallaa
and if i change the second sstring even that its showing me mohitbhallaa -
yamrajbhallai did it thanks aa lott
but
now
the main problem
for overloading with - operator
if my frst string s1 = mohitbhalla and
second string s2 =bhalla
than
string s3=mohit; -
yamrajbhallaplease rply ppl
i need it and i still nt on it -
pradeep_agrawalIts always a good practice to first try to solve the problem and then get help if stuck somewhere. We will be more than happy to help in that case.
Could you please post code for overloading the '-' operator for the problem statement and the problem you are facing in getting the desired result.
Note: The logic to solve the issue can be to parse the string s1 for string s2 and if s2 is found then remove s2 from s1 by moving the characters after s2 to the index starting from s2 location.
-Pradeep -
yamrajbhallanot getting the desired results
string s1=cooperhead;
string 2 = head;
s3=s2-s1;
but
output is
cooperhead
if u have logic than reply please
b operator -(b c) { b t; char tm[50]; for(int i=0;i<=(strlen(nm)+1);i++) { for(int j=0;j<=(strlen(c.nm)+1);j++) if (nm[i]!=c.nm[j]) tm[i]=nm[i]; } strcpy(t.nm,tm); return t;
-
pradeep_agrawalIf found multiple issue with the below code:
b operator -(b c) { b t; char tm[50]; for(int i=0;i<=(strlen(nm)+1);i++) { for(int j=0;j<=(strlen(c.nm)+1);j++) if (nm[i]!=c.nm[j]) tm[i]=nm[i]; } strcpy(t.nm,tm); return t;
The issues are:
- You are running a loop from 0 to (strlen of string + 1). The (strlen + 1) represent some garbage character. The loop should be run for 0 to i (and j) < (strlen of string).
- After above changes the problem will stuck due to inner loop always starting checking from 0. This will assign the whole string of s1 to temprary array tm (except the characters present in s2), e.g., if s2 is "cooperhead" and s2 is "head", the updated code will result in a string "coopr", i.e., an extra e will get deleted. For this the inner loop should be modified to run from "i to < strlen" and ignore characters in the s1 only if the complete string s2 is found and not if any character of s2 is found.
- After above changes the strcpy function may throw error because the string array "tm" is not ended by end of string character '/0'. You should do that before the stcpy statement as "tm = '/0';"
Try above changes and I feel that should make your code working.
Let us know if you face issue even after above changes.
A query on the problem statement
1. Will s2 always appear at end of s1 like "cooperhead" has "head" at the end. If this is the problem statement then the problem can be more easily solved by checking if s2 is present at end of s1 and if yes the just copy s1 to tm and do "tm[strlen(s1) - stelen(s2)] = '/0';"
2. Or can s2 be anywhere in s1, e.g, s1 is "cooperhead-head2"? If this is the problem statement then the approach you are currently following should be followed. But then depending on what should be removed on running the program as
- all instances of "head"?
- or only first instance of "head"?
- or only last instance of "head"?
the code will change.
-Pradeep
You are reading an archived discussion.
Related Posts
Hi, here comes another one. Connect them..
happy connecting.
hi,
this is srikanth .I have a problem with applets.Program successfully compiled but appletviewer cant be initialized my browser also opens a blank page what is the problem and how...
if i have rear wheels car with a diagonal split the pressure in the 2 lines is the same?if i measure this 2 output lines i must find the same...
Can any one please give me the link to download a free e book for BSNL JTO Preparation Exam
[This is a paid job]
CEans,
If anyone among you can develop plugins for wordpress (and buddypress); I have a simple project for you that won't take more lot of...