473,396 Members | 2,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

c# - return string concatenated with itself, without exceeding limit

Hi,

I have attempted to answer this question but where have I gone wrong?

Return the given string doubled (concatenated with itself) , it should be doubled as many times as possible without exceeding the length given by limit. The string should be doubled a minimum of once, regardless of limit.




Expand|Select|Wrap|Line Numbers
  1.         public static string DoubleString(string aString, int limit)
  2.         {
  3.  
  4.  
  5.             do
  6.             {
  7.                string.Concat(aString, aString);
  8.             } while (aString.Length <= limit);
  9.             return aString;
  10.  
  11.  
  12.  
  13.         }
  14.     }
  15.  
Jul 17 '17 #1
2 1705
Luk3r
300 256MB
This code will literally concatenate the string onto itself. (Example: If the string is 'test' and the limit is 2, the result will be 'testtest'). You were very close.

Expand|Select|Wrap|Line Numbers
  1.         public static string DoubleString(string aString, int limit)
  2.         {
  3.             limit = aString.Length * limit;
  4.             do
  5.             {
  6.                 aString += aString;
  7.             } while (aString.Length < limit);
  8.             return aString;
  9.         }
Jul 18 '17 #2
Frinavale
9,735 Expert Mod 8TB
Luk3r, you're close but in the end aString will be greater than the limit because it needs to be longer than the limit in order to exit your while.

You could do this:
Expand|Select|Wrap|Line Numbers
  1. public static string DoubleString(string aString, int limit)
  2. {
  3.   int doublingLimit = aString.Length * limit;
  4.   string result = aString;
  5.   do
  6.   {
  7.     result += result ;
  8.   } while ((result + result).Length < doublingLimit);
  9.   return result ;
  10. }
  11.  
Or this:
Expand|Select|Wrap|Line Numbers
  1. public static string DoubleString(string aString, int limit)
  2. {
  3.   int doublingLimit = aString.Length * limit;
  4.   string result = "";
  5.   string check = aString;
  6.   do
  7.   {
  8.     result = check;
  9.     check += aString;
  10.   } while (check).Length < doublingLimit);
  11.   return result ;
  12. }
  13.  
However, I'm not really sure why we are doing any calculation with limit...the limit is a hard value according to the specs: ...without exceeding the length given by limit.

So it really should be:
Expand|Select|Wrap|Line Numbers
  1. public static string DoubleString(string aString, int limit)
  2. {
  3.   string result = aString;
  4.   do
  5.   {
  6.     result += result ;
  7.   } while ((result + result).Length < limit);
  8.   return result ;
  9. }
  10.  
Or this:
Expand|Select|Wrap|Line Numbers
  1. public static string DoubleString(string aString, int limit)
  2. {
  3.    string result = "";
  4.   string check = aString;
  5.   do
  6.   {
  7.     result = check;
  8.     check += aString;
  9.   } while (check).Length < limit);
  10.   return result ;
  11. }
  12.  
Unless the value provided as limit is really the number of times it can be doubled...in which case we could simply do:
Expand|Select|Wrap|Line Numbers
  1. public static string DoubleString(string aString, int limit)
  2. {
  3.   string result = "";
  4.   int counter = 0;
  5.   do
  6.   {
  7.     result = check;
  8.     check += aString;
  9.   } while (counter < limit);
  10.   return result ;
  11. }
  12.  
But I really don't think this is the intention because of the specs provided.


Regardless, my point is that you cannot use the value that you will be returning to check against the limit as a condition to exit the while loop because it must be larger than the limit in order to exit the loop which is not what you want. You really want the value you are returning to be 1 iteration less than the exit condition.

-Frinny
Aug 4 '17 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: net | last post by:
I know how to make a page open itself at a certain size and location - I use: <html> <head><script language="Javascript"> window.resizeTo(370,220); window.moveTo(10,10); </script> <meta...
4
by: rod | last post by:
Hello SQL gurus! I am trying to write a query that will return a set of continguous rows from a table, and limit the number of rows returned when a maximum total has been reached by adding a...
2
by: Jacques Koorts | last post by:
Hi, I've managed to get the parent window close the child window (popup). I do this by the child calling the parent to close it. How can the child close itself without help from parent? So...
3
by: James DeClerk | last post by:
Hi everyone. I have a guid that's in string format. I would like to convert it to a GUID. I'm doing this on the unix platform, so it will apply to only to GCC/G++. #include "stdafx.h"...
1
by: Daylor | last post by:
how can i return string to .Net application from c dll ? i have the text in char sText ; and i want to return it from c dll to .Net application. i tried : *char in the function, but the...
10
by: muntyanu | last post by:
Hi all, In C# system service I need to get some string from MFC mainframe window by SendMessage() API call. I managed to send message, but can not find the way to return string to C# app. Is...
1
by: Scanner2001 | last post by:
I am looking for a way to return the output of a web service as a string without the xml tags, just the raw data. I am calling the web service from an html page using a form post method....
18
by: Ed Jay | last post by:
<disclaimer>js newbie</disclaimer> My page has a form comprised of several radio buttons. I want to poll the buttons to determine which button was selected and convert its value to a string. I...
17
by: streamkid | last post by:
Why can't a function not return string? (error: new types may not be defined in a return type)... How can i solve it? I thought of passing the string into the function, but that will make me...
10
by: amoharir | last post by:
Hello experts, I heard there is a way to print a string in console without using any print statement. (e.g printf, cout, clog, cerr, puts and similar commands) I got a hint to use if...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.