3

I am given a string array and I have to create a new string array out of it with strings that are, say, less than 3 characters long. The problem is that I am not allowed to use lists, so everything has to be done with arrays.

So far I managed to filter the needed strings out of the array, but I dont know how to put them into a new array. Again, using lists is not allowed and it has to be a new array, so I cant modify the already existing one.

String[] string_array1 = new String[10] {"Monday", "Tue", "Wed", "Thu", "Fri", "Saturday", "Sunday", "Pizza Day", "Catt", "Garbage Day"};

Console.WriteLine(String.Join(", ", string_array1));

String[] string_array2 = new String[i];
for (int i = 0; i < string_array1.Length; i++)
{   
    if (string_array1[i].Length < 4)
    {
        Console.Write($"{string_array1[i]}" + " ");
    }
}
5
  • is it had to be array or could you use something like List? does linq allowed? my concerns are, an array has fixed size, while List allows arbitrary length. if you insist on using array, you should find the number of elements that fulfil your criteria and then initialize the array for that size before finally filling the array up. Commented Jul 28, 2023 at 11:33
  • 1
    String[] string_array2 = string_array1.Where(s => s.Length <= 3).ToArray() - No lists used. Commented Jul 28, 2023 at 11:37
  • @David that uses linq, before manifesting them into an array. i am curious whether op is doing some codility stuff or just daily use. if its codility thingy, op have to use iterations like the good old days. Commented Jul 28, 2023 at 11:38
  • It's unclear what you're asking. Can you show us the output given the new String[10] {"Monday", "Tue", "Wed", "Thu", "Fri", "Saturday", "Sunday", "Pizza Day", "Catt", "Garbage Day"} input? Commented Jul 28, 2023 at 11:38
  • @BagusTesa: Indeed it does make use of LINQ. Which isn't a restriction mentioned by the OP. We can make guesses about the academic nature of the OP's requirement. But if it's an academic problem then the OP is likely expected to solve it. (I can't speak to whether or not LINQ makes use of generic lists under the hood, but if the OP is also restricted from relying on anything that internally relies on generic lists then the OP has considerable research and testing to do.) Commented Jul 28, 2023 at 11:43

3 Answers 3

3

One option would be to use intermediate List<string>:

List<string> list = new();
for (int i = 0; i < string_array1.Length; i++)
{   
    if (string_array1[i].Length < 4)
    {
        Console.Write($"{string_array1[i]}" + " ");
        list.Add(string_array1[i]);
    }
}

var string_array2 = list.ToArray();

Or just use LINQ all the way:

var string_array2 = string_array1
   .Where(s => s.Length < 4)
   .ToArray();

If you want to only use arrays then use intermediate indexer/counter:

string[] string_array2 = new string[string_array1.Length];
var counter = 0;
for (int i = 0; i < string_array1.Length; i++)
{   
    if (string_array1[i].Length < 4)
    {
        string_array2[counter++] = string_array1[i];
        Console.Write($"{string_array1[i]}" + " ");
    }
}

And then optionally resize the array - Array.Resize(ref string_array2, counter); (so it does not contain extra null's)

Sign up to request clarification or add additional context in comments.

2 Comments

OP mentioned he is not allowed to use Lists. The second option could be valid
@Jonathan code was updated to incorporate array-only solution
2

This works and does not use lists or LINQ:

String[] string_array2 = Array.FindAll(string_array1, x => x.Length < 4);

That gives:

output

Comments

0
String[] string_array2 = new String[i];

I don't think you can use "i" here for the length of the array as you haven't declared the variable yet and when you do you declare it as a local variable inside the for loop.

String[] string_array1 = new String[10] {"Monday", "Tue", "Wed", "Thu", "Fri", "Saturday", "Sunday", "Pizza Day", "Catt", "Garbage Day"};

Console.WriteLine(String.Join(", ", string_array1));

String[] string_array2 = new String[string_array1.length];
for (int i = 0; i < string_array1.Length;)
{   
    if (string_array1[i].Length < 4)
    {
        string_array2[i] = string_array1[i];
        Console.WriteLine(string_array2[i]);
    }
    i++;
}

I've moved the incrementation to the end of the loop so it will run once prior to incrementing "i" so as to access the element at position 0 in both arrays. Inside the If statement we set the value of string_array2 to be equal to the value of string_array1 if the length of the value in string_array1 is less than 4 characters in length.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.