static void Main(string[] args)
{
string path = @"F:\file.txt";
string text = System.IO.File.ReadAllText(path).ToLower();
string[] arr = text.Split(new char[] { '.', '?', '!', ' ', ';', ':', ',' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string s in arr)
{
Console.WriteLine(s);
}
Console.WriteLine("BEFORE:");
for (int i = 0; i < arr.Length; i++) Console.WriteLine("{0}. {1}", i + 1, arr[i]);
for (int i = 0; i < arr.Length; i++)
{
for (int j = 0; j < arr.Length - 1; j++)
{
if (needToReOrder(arr[j], arr[j + 1]))
{
string s = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = s;
}
}
}
Console.WriteLine();
Console.WriteLine("AFTER:");
for (int i = 0; i < arr.Length; i++) Console.WriteLine("{0}. {1}", i + 1, arr[i]);
Console.ReadKey();
}
protected static bool needToReOrder(string s1, string s2)
{
for (int i = 0; i < (s1.Length > s2.Length ? s2.Length : s1.Length); i++)
{
if (s1.ToCharArray()[i] < s2.ToCharArray()[i]) return false;
if (s1.ToCharArray()[i] > s2.ToCharArray()[i]) return true;
}
return false;
}
String text = "aaaa aa aaaaa aaa a aaaaaaaaa aaaaaaaaaa aao";
char searchFor = 'a';
var sorted = text
.Split(new char[] {'.', '?', '!', ' ', ';', ':', ','}, StringSplitOptions.RemoveEmptyEntries)
.ToList()
.OrderBy(s => s.ToCharArray().Count(c => c == searchFor))
.ToList();
sorted.ForEach(s => Console.WriteLine(s));
class Program
{
static void Main(string[] args)
{
string path = @"E:\file.txt";
string text = System.IO.File.ReadAllText(path).ToLower();
string[] array = text.Split(new char[] { '.', '?', '!', ' ', ';', ':', ',' }, StringSplitOptions.RemoveEmptyEntries);
int[] countArray = new int[array.Length];
char letter = 'o';
for (int i = 0; i < array.Length; i++)
{
for (int j = 0; j < array[i].Length; j++)
{
if (array[i][j] == letter)
{
countArray[i] += 1;
}
}
}
for (int i = 0; i < countArray.Length - 1; i++)
{
for (int j = i + 1; j < array.Length; j++)
{
if (countArray[j] < countArray[i])
{
var temp = countArray[i];
string tempSTR = array[i];
countArray[i] = countArray[j];
countArray[j] = temp;
array[i] = array[j];
array[j] = tempSTR;
}
}
}
for (int i = 0; i < array.Length; i++)
{
//System.Console.Write(countArray[i] + "");
System.Console.Write(array[i]+" ");
}
Console.ReadKey();
}
}