[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

c# - help sequence

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying 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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

649 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