Vedanshu
Vedanshu

Reputation: 627

How can I write a conditional without any conditional statements or operators?

My computer teacher wants me to write code to concatenate two strings( in a weird way). The code should be such that if length of both string are equal then output should be (string1 + string2). Otherwise the output should be string which is larger in length. Challenge is that I should not use if else statements or condition?exp1:exp2 whatsoever. This is what I am able to come up with (a and b are names of input string):

int aLen = a.Length;
int bLen = b.Length;
//+1 is added to lengths to prevent divide by zero 
int bGreatFlag = ((aLen+1) % (bLen + 1)) / (aLen + 1);  //1 if aLen < bLen; 0 Otherwise
int aGreatFlag = ((bLen+1) % (aLen+1)) / (bLen+1);  //1 if bLen < aLen; 0 Otherwise
string result = (a + b).Substring((bGreatFlag) * aLen,(aLen + bLen)-(bGreatFlag*aLen)-(aGreatFlag*bLen));

I believe that there is another way to approach this question which I am missing altogether(an inbuilt function or some LINQ maybe?). Any other approach or any pointers in the right direction to join strings conditionally will be really helpful. Thanks :) . Please be kind if the answer to this is very trivial.

Upvotes: 4

Views: 150

Answers (5)

Helio
Helio

Reputation: 613

you can use Math.Sign to determine which length is greater. Hope this helps...

string a, b, result;
switch (Math.Sign(a.Length - b.Length))
{
    case 1: result = a; break;
    case -1: result = b; break;
    default: result = a + b; break;
}

Upvotes: 0

StuartLC
StuartLC

Reputation: 107277

As per other's comments, your teacher doesn't exclude all the branches possible, e.g. you could use a while and break after one loop. switch would be my favourite, with the use of CompareTo() to reduce the range of output to -1, 0 and 1:

     switch ((s1.Length - s2.Length).CompareTo(0))
     {
        case -1: // s1 less than s2
           return s2;
        case 1: // s1 greater than s2
           return s1;
        case 0: // equal length
           return s1 + s2;
     }

Upvotes: 0

Jevgeni Geurtsen
Jevgeni Geurtsen

Reputation: 3133

Since I liked your way of doing it I improved it a bit for you:

            string a = "strin";
        string b = "string";
        string combined = (a + b);

        int aLength = a.Length; 
        int bLength = b.Length; 
        int aCheck = (aLength % bLength) / aLength;  
        int bCheck = (bLength % aLength) / bLength; 

        // If aCheck and bCheck are both 0 (same length) it isn't going to add any characters from the substring as
        // aLength * 0 = 0. If either aCheck or bCheck is 1 then it will add the length from aLength again to the string.
        string result = combined += combined.Substring(0, (aLength * (aCheck + bCheck)));
        result += combined.Substring(0, (bLength * bCheck)); ;

Upvotes: 0

H.C
H.C

Reputation: 608

try using while like this

        string r = "bla", rr = "blabla";
        while (rr.Length > r.Length)
        {
            MessageBox.Show("it works");
            break;
        }

where the r and rr is a example of the two strings and when it enters the break serves to make it run only once so it would work like a if...

Upvotes: 0

sloth
sloth

Reputation: 101072

Since you're allowed to use LINQ, here's a possible solution:

But your strings into a collection, group it by the length of its string, order the result by the length of the strings, then take the group with the longest strings. Since now you have a collection of either both strings (if they are of equal length) or the longer one, create a string of this collection by using String.Join.

Spoiler (don't miss the fun of implementing this yourself):

var result = String.Join("", new[]{a, b}.GroupBy(x => x.Length).OrderByDescending(x => x.Key).First().ToArray());

Upvotes: 3

Related Questions