Getting individual string length in an array [closed] - c#

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am trying to write a program which does the following:
There is a method called lengthOffTheWords. It receives an array of strings, and returns an array of numbers which represent the length of each individual string.
Ex: For the following input
{"I", "know", "a" , "friend"} the method returns {1,4,1,6} .
{"yes"} the method returns {3}.
{"me", "too"} the method returns {2,3}.
I would like to see an example of how to write it.

I would do something like this:
public int[] LengthOffTheWords(string[] array)
{
return array.Select(item => item.Length).ToArray();
}
I did not test this but it should do what you want.

int[] GetLengths(string[] array)
{
int[] structure = new int[array.Length];
for(int i = 0;i < array.Length;i++)
{
int length = array[i].Length;
structure[i] = length;
}
return structure;
}

Related

How to sort by name an Enum in c# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
Improve this question
I was trying to sort an Enum by the names, I have the following code:
Enum.GetNames(x.options).Where(.....).Select(.....)
notice that x.options is an enum
and I would like to sort de results of the linq by the name of the Enum, how could I do it?
Assuming the following enum:
public enum Number
{
One = 1,
Two = 2,
Three = 3,
Four = 4,
}
You need to pass the enum's type in Enum.GetNames and then call OrderBy (to sort in ascending order):
var sortedEnum = Enum.GetNames(typeof(Number)).OrderBy(x => x);
// or, from .NET 5 onwards:
var sortedEnum = Enum.GetNames<Number>().OrderBy(x => x);
This will return an IOrderedEnumerable<string> having the following (sorted) values:
Four
One
Three
Two
Of course you could .ToList() or .ToArray() the results, to return them in a list or an array, respectively.

Convert an entire string value into ASCII number in ASP.NET Core [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 months ago.
Improve this question
I need to convert the entered string value into an ASCII value to store into database as an array object. It is required as per the functional requirement.
You can use the below code -
static void Main(string[] args)
{
string value = "Hello World";
Console.WriteLine(value);
int[] result = new int[value.Length];
int index = 0;
foreach (var c in value)
{
if (char.IsDigit(c))
{
result[index] = Convert.ToInt32(c);
}
else
{
result[index] = ((int)c);
}
index++;
}
foreach (int i in result)
{
Console.Write("{0} ", i);
}
}
The if part is added due to consider if the string value contains any numerical value. That's why it will check either that particular character is digit or not. If digit then it will cast the value into Integeer type. Otherwise, else block will be called for all the characters value.

How to number values in string array with int [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have some strings in string array.
Now I wanna to give before first string prefix 1: string..etc
Example:
Test
Test
Test
Expected result:
1. Test
2. Test
3. Test
Is that even possible? Thanks!
You could use Enumerable.Select (Linq) with Index, and use string formatting to include Index as prefix of string.
For Example
var result = strCollection.Select((x,index)=> $"{index+1}.{x}");
If you want to do this in-place with an array of strings (without creating a new array), you can't use Linq. Instead, just use something like this:
public static void AddNumberingPrefix(string[] strings)
{
for (int i = 0; i < strings.Length; i++)
{
strings[i] = $"{i+1}. {strings[i]}";
}
}
Example usage:
string[] items = {"First", "Second", "Third"};
AddNumberingPrefix(items);
Console.WriteLine(string.Join("\n", items));
This outputs:
First
Second
Third
There is an overload of LINQ Select that gives you the index:
yourList.Select((el, idx) => $"{idx+1}. {el}");

Sort student array according to average marks [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have created student class array in C# like this
var Student = new Student[5];
having variables sid,name, avgMrks;
I want to sort array according to average marks of all the students.
I assuming when you say having variables, you mean that the Student object has the properties: sid, name, avgMrks. You can do:
Student.OrderBy (x=>x.avgMrks);
Use LINQ
Student = Student.OrderByDescending(c => c.avgMrks).ToArray();
It returns IOrderedIEnumerable, which you can convert back to Array if you want.
Or
string[] ArrStr = new string[] { "A", "A2", "A1" };
Array.Sort(ArrStr);
Array.Reverse(ArrStr);

How to pass array to method [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
It's probably a stupid question, but anyway.
My problem is that I can't pass uninitialized array, but I don't know if my array need to hold 5 or 30000 elements, eg. so it will be a waist of memory to initialize big array.
Should I use List<T> instead, or?
I've noticed people tend to return array instead of list, which is mutable, and therefore much more convenient, so there must be a performance issue with lists. Is that so?
make it into an 'out' parameter and all should be well:
private void x()
{
string sTestFile = "this is a test";
string[] TestFileWords;
FixConcatString(sTestFile, out TestFileWords);
}
private void FixConcatString(string splayfile, **out** string[] sWordArray)
{
char[] charSeparators = new char[] { '-' };
splayfile = splayfile.ToLower();
splayfile = splayfile.Replace(#"\", " ");
sWordArray = splayfile.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries);
}

Categories