#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];
      }
    }
}

Thanks for posting your code. Can you tell us, what error are you getting?

Yeah, 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 in particular strcpy().

The C++ string class is probably a better way to go, but I assume you're using C-style strings because you've been asked to.

Member Avatar for iamthwee

^^ 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 just pretend you're writing c-style only code and then you can't really go wrong. Well this is not strictly true but the code is trivial so it won't matter.

I THInk U should eneter the 2 strings using loops (for)

commented: Bad suggestion. -3

or u should use gets() to enter the string

commented: Even worse suggestion. +0
#include<iostream.h>
#include<conio.h>
#include<string.h>
int main()
{
  clrscr();
   int i,j;
  char  string1[20]="AMITABH BACHCHAN";
   char string2[20]="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<<string1[i];
      }
    }
    getch();
    }

Just 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 of string2 for each element in string1? For instance, you can take advantage of the fact that you don't care about the order that the characters appear in; just that they appear at all...

@ ravenous

Are you talking about hashmaps ? Or map in C++ ?

@ rajat

You should never use gets() for its undefined behaviour. Use fgets() instead. See this.

commented: gets() does not have *undefined* behaviour. It has *dangerous* behavior. +14

@ waltP

Oh sorry ! :( my bad. Yes, in my university, they have fedora installed. Once I used gets() and when I compiled it, the compiler said, something like "dangerous to use gets()".

Are you talking about hashmaps ? Or map in C++ ?

Don't think so. Just about sorting both strings and the determining their set intersection.

char amit[] = "AMITABH BACHCHAN" ;
char rajn[] = "RAJNIKANTH" ;
std::sort( std::begin(amit), std::end(amit)-1 ) ;
std::sort( std::begin(rajn), std::end(rajn)-1 ) ;
std::set_intersection( std::begin(amit), std::end(amit)-1, 
                       std::begin(rajn), std::end(rajn)-1,
                       std::ostreambuf_iterator<char>(std::cout) ) ;

Nope, sorting is N Log(N), you can do it in N + M...

sorting is N Log(N), ...

Use a counting sort with std::vector< char > bucket( std::numeric_limits<unsigned char>::max() ) ;
http://en.wikipedia.org/wiki/Counting_sort

O(N) time, O(M) space where M is the range of values a char can hold.

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
commented: I don't think vectors, maps, numeric_limits and sorts are crap. +6

Note for those facing challenges reading comprewhension:

ravenous said: Nope, sorting is N Log(N), you can do it in N + M...

And I believe that he He can define a char string, and use vectors, maps, numeric_limits, sorts, and all that crap.

Though I must admit that it may not be true of Mr. WaltP; these are and will remain 'crap' for someone who has proclaimed his belief that std::sort() is a 'language extention'.

I just wrote a really long reply to this, explaining that Vijayan121's answer was techincally quite correct, but excepting that the OP might be unfamiliar with (and therefore confused by) the full technical details of it. However, I lost it because my keyborad has a stupid shortcut "back" button that I accidentally pressed instead of an arrow key :( Anyway,

std::vector< unsigned > buckets( std::numeric_limits< unsigned char >::max() );

(note the correction to the type of the vector ;) ) is basically equivalent to:

unsigned letterCounts[ 256 ];

for the purposies of an exercise like this. The trick is that you can convert an ASCII character to an unsigned int between 0 and 255; since the char type in C++ is a single byte, so there are only 2^8 = 256 possible values that a char can take. OK, so you can go through the first string and count the number of each type of character by adding one to the correct bin in the letterCounts array for each letter in the string:

for ( unsigned i = 0; i < iStringSize; ++i )
    letterCounts[ static_cast< unsigned char >( string1[i] ) ]++;

Don't forget to fill the counts array with zeros first :)

Once you have these counts, you can simply go through the second string and check the entry in the letterCounts array for each letter in the second string. If there is a non-zero value for the count of any letter, you know it's common to both strings.

You could extent this method to doing a case-insensitive compare, or comparing only alpha-numeric characters using the std::tolower and std::isalnum from the cctype header file.

Have fun

std::vector< unsigned > buckets( std::numeric_limits< unsigned char >::max() );
(note the correction to the type of the vector ;)

unsigned does not improve it in any way.

If it is to be a correction, it would have to be:
std::vector< std::string::size_type > buckets( /* ... */ ) ;

unsigned does not improve it in any way.

It just means you can count more letters in each bin than if it's vector of char. std::string::size_type is probably better though (well, equivalent on a 32-bit system and bigger on a 64-bit system) :) I fear this has all got a bit off-topic though!

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.