1

I'm doing a school exercise where the user inputs a string and the program must check if it's a palindrome. My only problem currently is that I can't get the loop to ignore whitespaces included in the input string.

Console.Write("Insert string: ");
string input = Console.ReadLine();
char[] charArray = new char[input.Length];

for (int i = 0; i < input.Length; i++)
{
    if (Char.IsWhiteSpace(input, i))
    {
        continue;
    }
    else
    {
        charArray[i] += input[i];
    }
}
string original = new string(charArray);

I've seemingly tried everything I know, but the whitespaces just get added to the array no matter what I try. Is there a simple solution for this?

1
  • what's the status of this question? if one solved it, then it should be used to be marked as solved for it. Commented Mar 29, 2018 at 12:58

4 Answers 4

1

[EDIT] Ok you can try the replace method which replaces what you provide with what you want instead (space into no space)

string str = "This is a test";
str = str.Replace(" ", "");
MessageBox.Show(str);
Sign up to request clarification or add additional context in comments.

4 Comments

I think the OP is asking about any whitespace - not just at the start and end. Palindromes ignore whitespace.
If I understood correctly, Trim() only trims the whitespace from the beginning and the end of the string, not any whitespace between words in that string, so sadly it doesn't help me with this one.
Also, strings are immutable, so you'd want to assign the return value of Trim() to something - input = input.Trim(); for instance
Alright you just got a bunch of answers! Choose your poison as they say
0

How about using the framework and going this route:

char[] charArray = input.Replace(" ", "").ToCharArray();

Comments

0

Maybe this could work ?

Console.Write("Insert string: ");
string input = Console.ReadLine();
char[] charArray = input.Where(character => !Char.IsWhitespace(character)).ToArray();

Comments

0

When a whitespace is encountered, you never update the value at its position and so it remains a whitespace. So, write to a new array/string:

var newString = string.Empty;

for(int i = 0; i < input.Length; i++)
{
    if(!Char.IsWhiteSpace(input[i]))
    {
        newString += input[i];
    }
}

or something more like your code:

Console.Write("Insert string: ");
string input = Console.ReadLine();
char[] charArray = new char[input.Length];
var newString = string.Empty;

for (int i = 0; i < input.Length; i++)
{
    if (Char.IsWhiteSpace(input, i))
    {
        continue;
    }
    else
    {
        newString += input[i];
    }
 }
Console.WriteLine(newString);

1 Comment

It's better to use StringBuilder than append to a string in a loop. Otherwise, performance will suffer significantly once the string becomes large.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.