Advertisements

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:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace ConsoleApplication3
{
    class Program
    {
        static public void charsorting(char[] process, int length)
        {
            for (int a = 1; a < length; a++)
            {
                for (int b = 0; b < length - a; b++)
                {
                    if (process[b] > process[b + 1])
                    {
                        char tempdata = process[b];
                        process[b] = process[b + 1];
                        process[b + 1] = tempdata;
                    }
                }
            }
        }

        static public bool Permuation(char[] permute, int length)
        {
            for (int c = length - 1; c > 0; c--)
            {
                if (permute[c - 1] >= permute[c])
                    continue;
                else
                {
                    if (c <= length - 3)
                    {
                        char charcheck = permute[c - 1];
                        int value = -1;
                        for (int j = length - 1; j >= c; j--)
                        {
                            if (charcheck < permute[j])
                            {
                                value = j;
                                break;
                            }
                        }
                        if (value == -1)
                            return false;
                        char character = permute[c - 1];
                        permute[c - 1] = permute[value];
                        permute[value] = character;

                        char[] process2 = new char[length - c];
                        for (int d = 0; d < length - c; d++)
                            process2[d] = permute[c + d];
                        charsorting(process2, length - c);
                        for (int e = 0; e < length - c; e++)
                            permute[c + e] = process2[e];
                        return true;
                    }
                    else
                    {
                        char[] dump = new char[3];
                        dump[0] = permute[permute.Length - 3];
                        dump[1] = permute[permute.Length - 2];
                        dump[2] = permute[permute.Length - 1];

                        int count = 3;
                        for (int f = count - 1; f > 0; f--)
                        {
                            if (dump[f - 1] >= dump[f])
                                continue;
                            else
                            {
                                if (f <= count - 2)
                                {
                                    if (dump[f + 1] > dump[f - 1])
                                    {
                                        char ch1 = dump[f + 1];
                                        dump[f + 1] = dump[f];
                                        dump[f] = dump[f - 1];
                                        dump[f - 1] = ch1;
                                    }
                                    else
                                    {
                                        char ch2 = dump[f - 1];
                                        dump[f - 1] = dump[f];
                                        dump[f] = dump[f + 1];
                                        dump[f + 1] = ch2;
                                    }
                                }
                                else
                                {
                                    char ch3 = dump[f];
                                    dump[f] = dump[f - 1];
                                    dump[f - 1] = ch3;
                                }
                                permute[permute.Length - 3] = dump[0];
                                permute[permute.Length - 2] = dump[1];
                                permute[permute.Length - 1] = dump[2];
                                return true;
                            }
                        }
                        return false;
                    }
                }
            }
            return false;
        }

        static void Main(string[] args)
        {
           
            string number;
            int counter = 0;
            string line;

            // Read the file and display it line by line.
            System.IO.StreamReader file =
               new System.IO.StreamReader("E:\\Mvc Test\\ConsoleApplication1\\ConsoleApplication1\\File\\test.txt");
            int count = 0;
            Console.WriteLine("Enter the number :");
            number = Console.ReadLine();
            if (number != null)
            {
              
                while ((line = file.ReadLine()) != null)
                {

                    string s = line + number;
                    char[] buffer = s.ToCharArray();
                    while (true)
                    {
                        Console.WriteLine(buffer);
                        count++;
                        if (Permuation(buffer, buffer.Length) == false)
                            break;
                    }


                }

                Console.WriteLine("\nCount: " + count);
                Console.ReadLine();
                counter++;
                file.Close();


                Console.ReadLine();

           
        }
            else
            {
                Console.WriteLine("Enter atleast one value..!!!");
                Console.ReadLine();

            }


        }
    }
}

File Path:

System.IO.StreamReader file = 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

 

Reference : Click here

Published : http://www.c-sharpcorner.com/Blogs/49159/

Advertisements

Leave a Reply

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

%d bloggers like this: