#include<iostream.h>
#include<conio.h>
#include<string.h>
int main()
{
clrscr();
char string1[20],string2[15];
int i,j;
string1="AMITABH BACHCHAN";
string2="RAJNIKANTH";
int n1=strlen(string1);
int n2=strlen(string2);
cout<<"common alphabets in AMITABH BACHCHAN AND RAJNIKANTH are-";
for(i=0;i<n1;i++)
{
for(j=0;j<n2;j++)
{
if(string1[i]==string2[j])
cout<<string[i];
}
}
}
madhusudan.srinivasan
0
Newbie Poster
Recommended Answers
Jump to PostThanks for posting your code. Can you tell us, what error are you getting?
Jump to PostYeah, I can see that there's a problem with these lines:
string1="AMITABH BACHCHAN"; string2="RAJNIKANTH";
Looks like you're not completely familiar with how to use C-style strings. Do you have a reference book you can use to look string manipulation? Perhaps look up the string.h header, and …
Jump to Post^^ Good.
Additionally, it looks like you're using the old turbo c.
Now don't quote me on this as I don't have it to test, std::strings may behave unusually or even NOT behave as expected at all.
If in doubt best to use a more modern compiler, if you can't …
Jump to PostJust a note. Your algorithm isn't particularly efficient. You can see from your loops that this algorithm requires
n1 * n2
iterations. You should consider if you need to loop over the whole ofstring2
for each element instring1
? For instance, you can take advantage of the fact that …
Jump to Post@ ravenous
Are you talking about hashmaps ? Or map in C++ ?
All 21 Replies
Bob
15
using namespace Bob
Team Colleague
madhusudan.srinivasan
0
Newbie Poster
Bob
15
using namespace Bob
Team Colleague
iamthwee
rajat.sethi93
-3
Newbie Poster
WaltP
commented:
Bad suggestion.
-3
rajat.sethi93
-3
Newbie Poster
WaltP
commented:
Even worse suggestion.
+0
rajat.sethi93
-3
Newbie Poster
rajat.sethi93
-3
Newbie Poster
ravenous
266
Posting Pro in Training
np complete
8
Newbie Poster
np complete
8
Newbie Poster
WaltP
commented:
gets() does not have *undefined* behaviour. It has *dangerous* behavior.
+14
rajat.sethi93
-3
Newbie Poster
np complete
8
Newbie Poster
vijayan121
1,152
Posting Virtuoso
ravenous
266
Posting Pro in Training
Tarick careem
0
Newbie Poster
vijayan121
1,152
Posting Virtuoso
WaltP
commented:
He can't even define a char string! How the h*** is he going to use vectors, maps, numeric_limits, sorts, and all that crap? Help him, don't flaunt your expertise
+0
m4ster_r0shi
commented:
I don't think vectors, maps, numeric_limits and sorts are crap.
+6
vijayan121
1,152
Posting Virtuoso
ravenous
266
Posting Pro in Training
vijayan121
1,152
Posting Virtuoso
ravenous
266
Posting Pro in Training
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.