0

I need help for this exercise:

Inside the Test class, you must create a public method called copyText that takes a text as input and answer with a text.

It must answer with the same text put together as many time as there is characters in the input text.

Ex: in("car") out "carcarcar";

Ex: in("it") out "itit";

Ex: in("love") out "lovelovelovelove";

Ex: in("coffe") out "coffecoffecoffecoffecoffe";

I have tried to make a solution, where I find the Length of the word,
but I can't figure out to do this part:

Answer with the same text put together as many time as there are characters:

 class Program
 {
    static void Main(string[] args)
    {
        Test k = new Test();            
        string carText = k.copyText("car");
        Console.WriteLine(carText.Length);
    }
}


class Test
{
    public string copyText(string text)
    {
         return text;
    }
}
PowerStat
  • 3,252
  • 7
  • 25
  • 47
  • with `carText.Length` you have the number of characters of the text, this is a good start, but this has to be used in the method `copyText`. do you feel at ease with loops ? – Cid Apr 14 '19 at 11:41
  • I am a total beginner at C# But i know the bassic of LOop Sorry for my english – Karsten Dall Sørensen Apr 14 '19 at 12:21

2 Answers2

0

You can loop through each character in the word, and append the text each time to a variable.

public string copyText(string text)
{
   string output = String.Empty;
   for(int i = 0; i < text.Length; i++) {
      output += text;
   }

   return output;
}
Cid
  • 13,273
  • 4
  • 22
  • 42
eXPresso
  • 78
  • 2
  • 9
  • 2
    Is it really helping to hand someone a homework solution? – Crowcoder Apr 14 '19 at 11:53
  • I agree with you, I suppose I should have just steered him in the right direction, but it always helped me to see an approach, if you do not have a reasonable approach. – eXPresso Apr 14 '19 at 12:21
  • Can i use something like this for Repeating text string? String.Concat(Enumerable.Repeat("---", 5)) I fund it here: https://stackoverflow.com/questions/3754582/is-there-an-easy-way-to-return-a-string-repeated-x-number-of-times?fbclid=IwAR2uEQXsvK5aVV3FMxpASrcGMGf4ZiOJPBs60mneEKsRl2_XQjzC-luTDNQ – Karsten Dall Sørensen Apr 14 '19 at 12:35
  • @KarstenDallSørensen Yes, you could outsource the content "---" in a variable, and the first Parameter in the method takes the string variable and the second one takes variable.Length. So you have always the right length of the added string. – eXPresso Apr 14 '19 at 13:52
0

It is good to point out that the String class is immutable and for every iteration new object will be created, which can cause high memory consumption. For concatenating strings see the StringBuilder class. I think this article will help you with your exercise.

PS: I also think you should not just copy the snippet, but really research and try to understand how it works. Then when you have more questions than answers, it will cause more and more research and actually learning.

Good luck with the coding :)