Click here to Skip to main content
15,893,594 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Here is the question and my answer (which is being shown as incorrect) below:
C#
{
        /****************************************************************
        
        Return the longest string possible by appending "abc" to the given string without exceeding the limit. If the string cannot be extended without exceeding the limit, return it unmodified.
        
        *****************************************************************/
        public static string AppendUntilLimit(string aString, int limit)
        {
            var fullLength = aString.Length + 3;

            if (fullLength <= limit)

            { return aString + "abc"; }
            else
            { return aString; }



        }
    }
}

Can anyone point out to me where I have gone wrong please?
Thanks in advance

What I have tried:

C#
public static string AppendUntilLimit(string aString, int limit)
        {
            var fullLength = aString.Length + 3;

            if (fullLength <= limit)

            { return aString + "abc"; }
            else
            { return aString; }



        }
    }
}
Posted
Updated 10-Jul-17 8:32am
v2
Comments
ZurdoDev 10-Jul-17 10:26am    
Looks good to me, at a quick glance. Why do you say it is wrong?
Member 13302374 10-Jul-17 10:35am    
When I press Ctrl+F5 to build and run this, I get a box saying:

Test Case failed:
Invoked method with (abc,9)
expected (abcabcabc)
received (abcabc)
Peter_in_2780 10-Jul-17 10:35am    
I think the question is to repeatedly append 'abc' without blowing the limit.
F-ES Sitecore 10-Jul-17 10:36am    
They might have meant for you to add abc repeatedly. So if text is "XYZW" and limit is 10 they want "XYZWabcabc". That's just a guess though.
Member 13302374 10-Jul-17 10:38am    
I see. Any ideas on how I'd code this?

Would something like this work for you?
C#
public static string AppendUntilLimit(string aString, string bString, int limit)
{
    return $"{aString}{bString}".Substring(0, limit);
}

If you want to prevent appending of bString you could use something like:
C#
public static string AppendUntilLimit(string aString, string bString, int limit)
{
    return aString.Length + bString.Length > limit ? aString : $"{aString}{bString}";
}
 
Share this answer
 
Comments
Member 13302374 10-Jul-17 10:37am    
Where did you get the bstring from? I get a red squiggly line underneath this
[no name] 10-Jul-17 10:39am    
It's in the updated method signature:

public static string AppendUntilLimit(string aString, string bString, int limit);

You can remove it and use hardcoded values if you like although it's not a practice that's recommended in the industry.
I suspect the problem you have here is the fact that you are missing some code; namely, you're missing the loop that you want to use to append the string onto. You call AppendUntilLimt, but it's only going to add one abc to the end because you haven't given it a loop to keep adding the string limit. So, you're going to have to modify your AppendUntilLimit method to look something like this:
C#
public static string AppendUntilLimit(string aString, int limit)
{
  bool finish = aString.Length + 3 < limit;
  while (!finish)
  {
    aString = aString + "abc";
    finish = aString.Length +3 < limit;
  }
  return aString;
}
 
Share this answer
 
You need to read carefully the statement, every word matters.
Quote:
Return the longest string possible by appending "abc" to the given string without exceeding the limit.

the word "longest" mean that if there is room for more than 1 "abc", you have to repeat the operation until there is no more room for another "abc".
 
Share this answer
 
v2
Your code seems ok to me, maybe they want you to do some more error checking, test for null, use try ... catch or something like that ?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900