Click here to Skip to main content
15,901,666 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Experts..

I want to eliminate the repeated strings in Two strings.
For Example If i give
First string ==> vijayadhas
Second string ==> nasrya

The out put should be vijdha nr

First String ==> arun prasath
Second String ==> ramya

The output should be ==> unprsath my

I try this code. But it is not working perfect

//string s1 = "vijayadhas";
//string s2 = "nasrya";
string s1 = "arunprasath";
string s2 = "ramya";
ArrayList name1 = new ArrayList();
ArrayList name2 = new ArrayList();

for (int i = 0; i < s1.Length; i++)
{
name1.Add(s1[i]);
}

for (int i = 0; i < s2.Length; i++)
{
name2.Add(s2[i]);
}

for (int i = 0; i < name1.Count; i++)
{
for (int j = 0; j < name2.Count; j++)
{
if (s1[i] == s2[j])
{
name1.RemoveAt(i);
name2.RemoveAt(j);
}
}
}

int len = name1.Count + name2.Count;
Console.WriteLine("Length of the remaing Chars==>"+len);

Console.WriteLine("\n\n Name 1==>");
for (int i = 0; i < name1.Count; i++)
{
Console.Write(name1[i]);
}
Console.WriteLine("\n\n Name 2==>");
for (int i = 0; i < name2.Count; i++)
{
Console.Write(name2[i]);
}


The out put for this program is ==> rnpraah y

Where i have to change to get the correct output?
Please help me.
Posted

C#
string s1 = "arunprasath";
string s2 = "ramya";
string result;

for (int xIx = 0; xIx < s1.Length; xIx++)
{
    for (int xKx= 0; xKx < s2.Length; xKx++)
    {
        if (s1[xIx] == s2[xKx])
        {
            s1= s1.Remove(xIx--, 1);
            // One -- to mutch
            // s2= s2.Remove(xKx--, 1);
            s2= s2.Remove(xKIx, 1);
            break;
        }
    }
}
result = s1 + " " + s2;
 
Share this answer
 
v6
Comments
[no name] 7-Dec-13 11:01am    
Thank you for accepting.
Please note also the comments/details on OriginalGriff solution. He is very right about efficiency.
Vijaydhas 7-Dec-13 11:10am    
Thank you.. It's Working and easy to understand. Thanks..
BillWoodruff 7-Dec-13 23:45pm    
Your answer is upvoted: good work ! But ... mmm ... I actually tested OriginalGriff's original solution ... the one using nested for loops and decrementing both loop counter variables in-line ... and it appeared to give the desired result on both examples supplied by the OP. Care to describe what you changed in detail ? thanks, Bill
[no name] 8-Dec-13 6:06am    
Thank you for voting. You can see what I changed, it is only this outcommented line where I replaced xK-- by xK which has no effect because of the following break.
I';m not sure why you think you need nested loops - all you need to do is look and see if there is a specific character in the string already.
Here is what I'd do:
C#
private string RemoveThese(string input, string remove)
    {
    foreach (char c in remove)
        {
        int found = input.IndexOf(c);
        if (found >= 0) input = input.Remove(found, 1);
        }
    return input;
    }
What it does is finds and removes all the characters in the remove string by checking if the IndexOf (which finds the first time it matches) is valid, and removing the character if it is.
All you then have to do is call the method twice:
C#
string output = RemoveThese(name1, name2) + RemoveThese(name2, name1);


(As it happens, that isn't the way I'd do it - it is horribly inefficient since it creates new strings left right and centre, since strings are immutable - but since I suspect you haven't got to the StringBuilder class yet, I didn't want to confuse you)
 
Share this answer
 
Comments
Vijaydhas 7-Dec-13 11:09am    
Thank you. It's working. I am new to program. That's why i have some faults.. Now only practicing for coding.. Thanks again..
[no name] 7-Dec-13 11:21am    
Your solution does not give the result OP is expecting. But anyway thank you for the hints about efficiency.
BillWoodruff 7-Dec-13 23:44pm    
For those with eyes to see, there is a Zen lesson in this code !

thanks, OG

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900