- moved half of word condition in for statement.
- converting word to upper moved out of for statement, so the word is processed only one time instead of unuseful n times.
using System; class Palindrome { public static bool Check(string word){ word = word.ToUpper(); for(int i = 0; i < word.Length / 2; i++) { if(word[i] != word[word.Length - 1 - i]) { return false; } } return true; } }
- using System;
- class Palindrome
- {
- public static bool Check(string word){
for(int i = 0; i < word.Length; i++)- word = word.ToUpper();
- for(int i = 0; i < word.Length / 2; i++)
- {
word = word.ToUpper();if(i >= word.Length /2){break;}- if(word[i] != word[word.Length - 1 - i])
- {
- return false;
- }
- }
- return true;
- }
- }