Click here to Skip to main content
16,020,305 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
foreach (string word in arrayList)
{
    pair = " ";
    for (i = 0; i <= word.Length - 1; i++)
    {
        if (i == word.Length - 1)
        {
            if (i != 0)
            {
                if (word[i] == word[i - 1])
                {
                    pair += word[i];
                    Console.Write(pair + "," + " - " + word + "\n");
                    pair = null;
                }
            }
        }
        else
        {
            if (i + 1 <= word.Length - 1)
            {
                if (word[i] == word[i + 1])
                {
                    pair += word[i];
                }
                else
                {
                    if (i != 0)
                    {
                        if (word[i] == word[i - 1])
                        {
                            pair += word[i];
                            Console.Write(pair + "," + " - " + word + "\n");
                            pair = null;
                        }
                    }
                }
            }
        }
    }
}


I try above code,
if string is : "success is my life" the output is
cc, - success
ss, - success
but i expect output is,
cc,ss, - success
Please help me, thank you
Posted
Updated 30-Sep-15 18:45pm
v2
Comments
Ralf Meier 1-Oct-15 0:18am    
You should memorise (with a helper-bool) that you found a double letter and if the double-letter-search is completed you check if the helper-bool is true. In this case you write the actual word also to the console ...
StM0n 1-Oct-15 0:48am    
Sorry to give you rather a mere suggestion, but maybe you should refactor your code a bit... especially the fact, that you got a huge code duplication... remove that and my guess would be, you will be able to obtain the solution for yourself :)
Thava Rajan 1-Oct-15 1:27am    
is there any other conditions, what about if there is more than one pair,
what about if the letter is in different position,
[no name] 19-Dec-15 12:13pm    
The answer was accepted. Why it is reverted back, is there any reason?

Something along this line. Note this will only check for character immediate to the current one.

C#
 string inp = "success is my life is success";
            string[] allWords = inp.Split(' ');

            var counts = allWords
    .GroupBy(w => w)
    .Select(g => new { Word = g.Key, Count = g.Count() })
    .ToList();

            var recurringWords = counts.Where(p => p.Count > 1).Select(p => p.Word).ToList();

            foreach (string recWord in recurringWords)
            {
                Console.WriteLine(recWord);
            }
/*Find character combinations*/
            List<string> resString = new List<string>();
            foreach (string thisWord in allWords)
            {
                char[] allChars = thisWord.ToCharArray();
                char[] charNext = allChars.Skip(1).ToArray();

                string combinations = "";
                
                for (int i = 0; i < allChars.Length-1; i++)
                {
                    if (allChars[i] == charNext[i])//combination found
                    {
                        combinations += allChars[i] + "" + allChars[i]+",";
                    }
                   
                }
                if (combinations!="")
                {
                    resString.Add(combinations + "-" + thisWord);
                }
                
            }
            foreach (string res in resString)
            {
                Console.WriteLine(res);
            }
            Console.ReadKey();</string></string>
 
Share this answer
 
v2
Try below code:
C#
string inputStr = "success is my life";
string[] arrayList = inputStr.Split(' ');

string pair = "";
Dictionary<string,string> tempList = new Dictionary<string,string>();

foreach (string word in arrayList)
{
	pair = " ";
	for (int i = 0; i <= word.Length - 1; i++)
	{
		if (i == word.Length - 1)
		{
			if (i != 0)
			{
				if (word[i] == word[i - 1])
				{
					pair += word[i];

					if (tempList.ContainsKey(word.Trim()))
					{
						tempList[word.Trim()] = tempList[word.Trim()] + "," + pair;
					}
					else
					{
						tempList.Add(word.Trim(), pair.Trim());
					}
					//Console.Write(pair + "," + " - " + word + "\n");
					pair = null;
				}
			}
		}
		else
		{
			if (i + 1 <= word.Length - 1)
			{
				if (word[i] == word[i + 1])
				{
					pair += word[i];
				}
				else
				{
					if (i != 0)
					{
						if (word[i] == word[i - 1])
						{
							pair += word[i];
							

							if (tempList.ContainsKey(word.Trim()))
							{
								tempList[word.Trim()] = tempList[word.Trim()] + "," + pair;
							}
							else
							{
								tempList.Add(word.Trim(), pair.Trim());
							}

							//Console.Write(pair + "," + " - " + word + "\n");
							pair = null;
						}
					}
				}
			}
		}
	}
}

foreach (var temp in tempList)
{
	Console.Write(temp.Value + " - " + temp.Key + "\n");
}

Note: I just modified partially your code and implemented logic as per your need.
 
Share this answer
 
v2

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