?
Solved

c# - help sequence

Posted on 2014-03-04
4
Medium Priority
?
223 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 2000 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

Interactive Way of Training for the AWS CSA Exam

An interactive way of learning that will help you visualize core concepts so that you can be more effective when taking your AWS certification exam.  Built for students by a student to help them understand the concepts that they are being taught.

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:…
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.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

770 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