Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trim a char array

Tags:

arrays

c#

Background: I was invited to an interview at a high profile company and I was asked the following question before being told I failed the interview for the position (C#,mvc3,razor). I'm genuinely interested in how to solve this.

Question: "Write a method that takes a char array, trims whitespace, and returns the same array." After some thinking I was told to replace the whitespace with "\o".

I started with:

public static char[] Trim(char[] c)
    {
        for (int i = 0; i < c.Length; i++)
        {
            if (c[i] == '\r' || c[i] == '\n' || c[i] == '\t')
            {
                c[i] = '\o';
            }     
        }
    }

I was told I have to use the same array, can't put it in a list and call ToArray(). However I think if the array stays the same size it is impossible to "trim it".

like image 310
The Muffin Man Avatar asked Jun 19 '12 22:06

The Muffin Man


People also ask

What is TRIM () in C#?

The Trim method removes all leading and trailing white-space characters from the current string object. Each leading and trailing trim operation stops when a non-white-space character is encountered. For example, If current string is ” abc xyz ” and then Trim method returns “abc xyz”.

How do you trim in C++?

boost::trim in C++ libraryThe trim function is used to remove all leading or trailing white spaces from the string. The input sequence is modified in place. trim_left(): Removes all leading white spaces from the string. trim_right(): Removes all trailing white spaces from the string.

How do I trim a space in C#?

Trim() Removes all leading and trailing white-space characters from the current string. Trim(Char) Removes all leading and trailing instances of a character from the current string. Trim(Char[]) Removes all leading and trailing occurrences of a set of characters specified in an array from the current string.

How do you trim strings?

The trim() method in Java String is a built-in function that eliminates leading and trailing spaces. The Unicode value of space character is '\u0020'. The trim() method in java checks this Unicode value before and after the string, if it exists then removes the spaces and returns the omitted string.


3 Answers

They may have meant \0 (NUL character), not dash-0

like image 151
Karthik Kumar Viswanathan Avatar answered Sep 28 '22 00:09

Karthik Kumar Viswanathan


Assuming they meant to replace whitespace characters with null characters then the solution is simple:

Step 1: From the start of the string (represented as a character array) replace whitespace characters until a non-WS character is encountered.

Step 2: From the end of the string, working backwards, do the same.

public static void Trim( Char[] chars )
{        
    int maxIdx = 0; // an optimization so it doesn't iterate through chars already encountered
    for( int i = 0;i < chars.Length; i++ )
    {
        if( Char.IsWhitespace( chars[i] ) )
        {
            chars[i] = '\0';
        }
        else
        {
            maxIdx = i;
            break;
        }
    }

    for( int i = chars.Length - 1; i > maxIdx; i-- )
    {
        if( Char.IsWhitespace( chars[i] ) ) chars[i] = '\0';
    }
}
like image 22
Dai Avatar answered Sep 28 '22 00:09

Dai


public static char[] Trim(char[] str)
{
  return str.Where(x => !Char.IsWhiteSpace(x)).ToArray();
}
like image 27
BlackSpy Avatar answered Sep 28 '22 01:09

BlackSpy