Strings Array Using Loops

Hi, In my code I’m trying to get the total of each sizes in my string using loops. For example i want to Output:
" Order of 3 Small shirts"
" Order of 2 Small shirts"
" Order of 1 Small shirts"
" Order of 2 Small shirts"
" Order of 1 Small shirts"


I tried and tried and I cant get the result i need. Plz help!

1 Like

If it were me, I would try using a variable to store the number of each shirt size. You could use either a series of if() statements or a switch() block to determine which variable to increment as you iterate over the array. For example:

string[]  socksInDrawer = new string[] {"white", "black", "black", "white", "black", "black", "white"};
int whiteSockCount = 0;
int blackSockCount = 0;

for (int i = 0; i < socksInDrawer.Length; i++) 
{
  if (socksInDrawer[i] == "white")
  {
    whiteSockCount++;
  }
  if (socksInDrawer[i] == "black") 
  {
    blackSockCount++;
  }
//if there were more sock colors, we would add more if()'s
}
Console.WriteLine("I have {0} white socks, and {1} black socks.", whiteSockCount, blackSockCount);

Or you could replace the if()'s with:

switch (socksInDrawer[i])
{
  case "white":
    whiteSockCount++;
    break;
  case "black":
    blackSockCount++;
    break;
}

Good luck! Post again, if you get stuck.

3 Likes

Thank you very much brother!

3 Likes

You’re quite welcome!

3 Likes