Composite Words using C#

Introduction

A composite word is a concatenation of two-or-more other words, such that the minimum length of each constituent word is 3.

In this article the program which accepts a list of words and an integer or string as input and prints the count of composite words which can be constructed by concatenating integer and string or more other words which are also present in the input.

Code: 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using System.IO;  
  7.   
  8. namespace ConsoleApplication3  
  9. {  
  10.     class Program  
  11.     {  
  12.         static public void charsorting(char[] process, int length)  
  13.         {  
  14.             for (int a = 1; a < length; a++)  
  15.             {  
  16.                 for (int b = 0; b < length - a; b++)  
  17.                 {  
  18.                     if (process[b] > process[b + 1])  
  19.                     {  
  20.                         char tempdata = process[b];  
  21.                         process[b] = process[b + 1];  
  22.                         process[b + 1] = tempdata;  
  23.                     }  
  24.                 }  
  25.             }  
  26.         }  
  27.   
  28.         static public bool Permuation(char[] permute, int length)  
  29.         {  
  30.             for (int c = length - 1; c > 0; c--)  
  31.             {  
  32.                 if (permute[c - 1] >= permute[c])  
  33.                     continue;  
  34.                 else  
  35.                 {  
  36.                     if (c <= length - 3)  
  37.                     {  
  38.                         char charcheck = permute[c - 1];  
  39.                         int value = -1;  
  40.                         for (int j = length - 1; j >= c; j--)  
  41.                         {  
  42.                             if (charcheck < permute[j])  
  43.                             {  
  44.                                 value = j;  
  45.                                 break;  
  46.                             }  
  47.                         }  
  48.                         if (value == -1)  
  49.                             return false;  
  50.                         char character = permute[c - 1];  
  51.                         permute[c - 1] = permute[value];  
  52.                         permute[value] = character;  
  53.   
  54.                         char[] process2 = new char[length - c];  
  55.                         for (int d = 0; d < length - c; d++)  
  56.                             process2[d] = permute[c + d];  
  57.                         charsorting(process2, length - c);  
  58.                         for (int e = 0; e < length - c; e++)  
  59.                             permute[c + e] = process2[e];  
  60.                         return true;  
  61.                     }  
  62.                     else  
  63.                     {  
  64.                         char[] dump = new char[3];  
  65.                         dump[0] = permute[permute.Length - 3];  
  66.                         dump[1] = permute[permute.Length - 2];  
  67.                         dump[2] = permute[permute.Length - 1];  
  68.   
  69.                         int count = 3;  
  70.                         for (int f = count - 1; f > 0; f--)  
  71.                         {  
  72.                             if (dump[f - 1] >= dump[f])  
  73.                                 continue;  
  74.                             else  
  75.                             {  
  76.                                 if (f <= count - 2)  
  77.                                 {  
  78.                                     if (dump[f + 1] > dump[f - 1])  
  79.                                     {  
  80.                                         char ch1 = dump[f + 1];  
  81.                                         dump[f + 1] = dump[f];  
  82.                                         dump[f] = dump[f - 1];  
  83.                                         dump[f - 1] = ch1;  
  84.                                     }  
  85.                                     else  
  86.                                     {  
  87.                                         char ch2 = dump[f - 1];  
  88.                                         dump[f - 1] = dump[f];  
  89.                                         dump[f] = dump[f + 1];  
  90.                                         dump[f + 1] = ch2;  
  91.                                     }  
  92.                                 }  
  93.                                 else  
  94.                                 {  
  95.                                     char ch3 = dump[f];  
  96.                                     dump[f] = dump[f - 1];  
  97.                                     dump[f - 1] = ch3;  
  98.                                 }  
  99.                                 permute[permute.Length - 3] = dump[0];  
  100.                                 permute[permute.Length - 2] = dump[1];  
  101.                                 permute[permute.Length - 1] = dump[2];  
  102.                                 return true;  
  103.                             }  
  104.                         }  
  105.                         return false;  
  106.                     }  
  107.                 }  
  108.             }  
  109.             return false;  
  110.         }  
  111.   
  112.         static void Main(string[] args)  
  113.         {  
  114.              
  115.             string number;  
  116.             int counter = 0;  
  117.             string line;  
  118.   
  119.             // Read the file and display it line by line.  
  120.             System.IO.StreamReader file =  
  121.                new System.IO.StreamReader("E:\\Mvc Test\\ConsoleApplication1\\ConsoleApplication1\\File\\test.txt");  
  122.             int count = 0;  
  123.             Console.WriteLine("Enter the number :");  
  124.             number = Console.ReadLine();  
  125.             if (number != null)  
  126.             {  
  127.                 
  128.                 while ((line = file.ReadLine()) != null)  
  129.                 {  
  130.   
  131.                     string s = line + number;  
  132.                     char[] buffer = s.ToCharArray();  
  133.                     while (true)  
  134.                     {  
  135.                         Console.WriteLine(buffer);  
  136.                         count++;  
  137.                         if (Permuation(buffer, buffer.Length) == false)  
  138.                             break;  
  139.                     }  
  140.   
  141.   
  142.                 }  
  143.   
  144.                 Console.WriteLine("\nCount: " + count);  
  145.                 Console.ReadLine();  
  146.                 counter++;  
  147.                 file.Close();  
  148.   
  149.   
  150.                 Console.ReadLine();  
  151.   
  152.              
  153.         }  
  154.             else  
  155.             {  
  156.                 Console.WriteLine("Enter atleast one value..!!!");  
  157.                 Console.ReadLine();  
  158.   
  159.             }  
  160.   
  161.   
  162.         }  
  163.     }  
  164. }  
File Path: 
  1. System.IO.StreamReader file =  
  2. new System.IO.StreamReader("E:\\Mvc Test\\ConsoleApplication1\\ConsoleApplication1\\File\\test.txt");  
Change your file path otherwise you got some error.right click the test.txt inside the file folder.then you get file path.

Output:

Output

Personal Blog.