Finding Common Alphabet Between Two Names.

We have two names, say “AMITABH BACHCHAN ” and “RAJNIKANTH”. Our work is find common alphabet in these two names. The answer would be ‘AITHN’. i have written my code, if you find your code, more easier than mine, please do write in comment box. So lets go!

If you are looking for the solution in C/C++. Please visit Here .

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace commonAlpha
{
    class Program
    {
        static void Main(string[] args)
        {
            char[] name1 = {'A','M','I','T','A','B','H','B','A','C','H','C','H','A','N'};
            char[] name2 = { 'R', 'A', 'J', 'N', 'I', 'K', 'A', 'N', 'T', 'H' };
            StringBuilder s1 = new StringBuilder();
            for (int i = 0; i<name1.Count(); i++)
            {
                for (int j = 0; j < name2.Count(); j++)
                {
                    //compareing two char from both string
                    if (name1[i] == name2[j])
                    {
                        char temp = name2[j];
                        if (s1.Length != 0)
                        {
                            int flag = 0;
                            //checking whether its already in s1 or not. If yes, flag will be greater than 0 or if not flag=0
                            for (int k = 0; k < s1.Length; k++)
                            {
                                if (s1[k] == temp)
                                    flag++;
                            }
                            //confirm that temp is not in s1
                            if (flag == 0)
                            {
                                s1.Append(temp);
                            }
                        }
                        else
                            s1.Append(temp);
                    }
                }
            }
            for (int i = 0; i < s1.Length; i++)
                Console.Write(s1[i]);
            Console.ReadLine();
        }
    }
}

Copyright © 2013 ·All Rights Reserved · Republishing or copying this article or any part of this without giving reference would come under WordPress Copyright Act.

If you liked this article, please rate and comment. Thank You!

Signature

Deepesh Singh
logo
You may also like.

Windows 8 App Store Internet on Windows Phone Influence of Online Social Networks C Program
Advertisements

13 thoughts on “Finding Common Alphabet Between Two Names.

  1. Add the PHP tags for the below code..
    $str1=”AMITABH BACHCHAN”;
    $str2=”RAJNIKANTH”;
    echo “The common alphabets in the two names are: “;
    $arr1=str_split($str1);
    $arr2=str_split($str2);
    $result=array_unique(array_intersect($arr1, $arr2));
    echo implode($result);

  2. for java ::

    public class checking {

    public static void main(String[] s){
    checking ck=new checking();
    String s1=”AMITABH BACHCHAN”;
    String s2=”RAJNIKANTH”;
    char[] ch1=s1.toCharArray();
    char[] ch2=s2.toCharArray();
    StringBuilder sb=new StringBuilder();

    for(int i=0; i<ch1.length;i++){
    for(int j =0;j<ch2.length;j++){
    if(ch1[i]==ch2[j]){
    int cflag =0; // here to reset again
    char c=ch1[i];
    for(int k =0; k<sb.length();k++){
    if(c==sb.charAt(k)){
    cflag++;
    }
    }

    if(cflag==0)
    {
    sb.append(c);

    }
    }
    }
    }
    System.out.println(sb);
    }

    }

Please leave your valuable comment.

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s