Solved

c# - help sequence

Posted on 2014-03-04
4
214 Views
Last Modified: 2014-03-24
Hi,

I need help, I have 4 characters: 1 2 3 4. I want to know how it is given as input. Like , 1234 or 123 or 234 or 124 or 134. If it is sequence like above then print "True" otherwise if sequence is reverse like 4321 or 432 or 421 or 431 then print "False". I can write code with If-else but want to know what is best and good way to too it.

Please help.

Thanks
0
Comment
Question by:josephm67
  • 2
4 Comments
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39904877
You could write an extension method that would encapsulate this logic:

public static class ExtensionMethods
{
    public static bool IsInSequence(this string value)
    {
        for (int i = 0; i < value.Length - 1; i++)
        {
            if (value[i] > value[i + 1]) return false;
        }

        return true;
    }
}

Open in new window


Then you would have a simple mechanism for invoking this logic:

string input = "1324";
bool sequenced = input.IsInSequence();

Open in new window

0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39906261
Just a small modification to line 5 above

for (int i = 0; i < value.Length - 2; i++)

to avoid index out of range exception.
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 39906659
@CodeCruiser

Thanks!  I realized this before writing the code, but apparently my brain didn't feel the need to transmit that bit to my fingers   = )
0
 

Author Closing Comment

by:josephm67
ID: 39951080
thx
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video discusses moving either the default database or any database to a new volume.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now