Lily P.

asked • 10/30/20

C# method returning a bool

Hi!

I am checking whether an input is a palindrome or not with the help of a method. However, I get error in the part of code bolded and underlined below:



public static bool Check (string input)

{

bool palindrome = false;

string checker = "";

for (int i = input.Length; i >= 0; i--)

{

checker = checker + input[i];

}

if (checker == input)

{

palindrome = true;

}

return palindrome;

}

static void Main(string[] args)

{

Console.Write("Write a word: ");

string input = Console.ReadLine();

Check(input);

if (palindrome = true)

{


}

}


}


Why does it not know what variable palindrome is? Havent I returned it in my Check method?? Pls help!!

1 Expert Answer

By:

Patrick B. answered • 10/30/20

Tutor
4.7 (31)

Math and computer tutor/teacher

Lily P.

Thank you for your answer: I have rewritten my code: static bool Palindrome (string input) { bool pal; string check = ""; for (int i = input.Length; i >= 0; i--) { check = check + input[i]; } if(check == input) { pal = true; } else { pal = false; } return pal; } static void Main(string[] args) { Console.Write("Write a word: "); bool palindrome = Palindrome(Console.ReadLine()); if (palindrome == true) { Console.WriteLine("Ordet är en palindrom"); } else { Console.WriteLine("Ordet är inte en palindrom"); } Console.ReadKey(); It still does not accept my code :(
Report

10/30/20

Lily P.

Oops it got messy, I think i will do a new question for my rewriting
Report

10/30/20

Patrick B.

Here ya go.... I rewrote the algorithm, which works like this: points at the first and last characters... while the pointers have not yet crossed { compares the characters at each location.. if they do not match, then it's not a palindrome increments/decrements the pointers } As I am using an online interpreter, it will not let me do Console I/O without throwing all kinds of excpetions and hissy-fits... But it does work! You can change the strings inside the Go() routine to test it. Thanks!
Report

10/31/20

Patrick B.

I believe you also ordered code that capitalizes every other word in the string. So there ya go!
Report

10/31/20

Lily P.

Wow, so amazing! Thank you so much sir! You are legendary
Report

10/31/20

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.