Printing consecutive characters in a line

Given a character array, print the consecutive sequence in same line and others in a new line. eg: ABCXYZACCD
o/p :
ABC
XYZ
A
C
CD

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "ABCXYZACCD";
            string temp = "";
            for (int i = 0; i < str.Length; i++)
            {
                temp = temp + str[i];
                if (i < str.Length - 1)
                {
                    if (char.ConvertToUtf32(str, i) + 1 == char.ConvertToUtf32(str, i + 1))
                    {
                        continue;
                    }
                    else
                    {
                        Console.WriteLine(temp);
                        temp = "";
                    }
                }
                else
                {
                    Console.WriteLine(temp);
                }
            }
        }
    }
}

Output
=======
ABC
XYZ
A
C
CD
Press any key to continue . . .
Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

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