Solved

c# - help sequence

Posted on 2014-03-04
4
221 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

Get Actionable Data from Your Monitoring Solution

Your communication platform is only as good as the relevance of the information you send. Ensure your alerts get to the right people every time with actionable responses. Create escalation rules that ensure everyone follows the process and nothing is left to chance.

Question has a verified solution.

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

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:…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

718 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