Solved

c# - help sequence

Posted on 2014-03-04
4
220 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 75

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 75

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

730 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