using System;
namespace Palindrome
{
class Program
{
static void Main(string[] args)
{
// Declare the variables
string backwards = string.Empty;
string phrase = " ";
Console.Write($"Enter a string and we'll check to see if it's a palindrome. ");
phrase = Convert.ToString(Console.ReadLine());
Console.WriteLine($"\nYou entered: {phrase} ");
Console.Write($"\nIn reverse, your string is: ");
char[] charArr = phrase.ToCharArray();
int increment = 1;
char reverse;
foreach (char c in charArr)
{
backwards = Convert.ToString(reverse = charArr[charArr.Length - increment]);
increment++;
Console.Write($"{backwards}");
}
Console.WriteLine($"\n{backwards}");
if (String.Equals(phrase, backwards))
{
Console.WriteLine($"\nThis is a palindrome!");
}
else
{
Console.WriteLine($"\nThis is not a palindrome");
}
}
}
}
在这段代码中,我试图测试用户提交的字符串是否是回文(单词的前后拼写相同。)我可以使字符串向后拼写,但是当我尝试测试“向后”变量表示用户提交的字符串中的第一个字符,而不是整个字符串的反向拼写。任何建议都会有所帮助!