[英]Making lettercheck not case sensitive
所以我需要讓這個代碼能夠計算一個指定的字母(在這種情況下是字母 E)。 我已經做到了計算在句子中輸入了多少次字母的程度,但是它區分大小寫。 現在它只計算大寫 E,可以更改,但我不知道它如何讀取大寫和小寫。
我找到了equalsIgnoreCase()
代碼,但是我不熟悉它,所以我不知道把它放在哪里,它是如何工作的,或者它是否會工作。 就我而言,我嘗試了myString.equalsIgnoreCase(MyString2)
,在我的情況下CharCount
和CharCountLow
我相信。
這是我的代碼:
//Written sentence gets read by arduino when enter is pressed and printed into the serial monitor
//Arduino counts how many characters are in the sentence and prints out that number in serial monitor
//Arduino then counts how many times any chosen letter in the sentence is written, for example E, and then prints out that number on the monitor.
//Arduino then reads the sentence you've written and prints it backwards in serial monitor
char Message[128] = "";
char MessageInversed[128] = "";
static int CharCount;
char NextChar;
char CountChar = 'E';
char CountCharLow = 'e'; // this is what I've tried
int Counter;
boolean Done;
void setup()
{
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("<Enter your text>");
}
void loop()
{
Readmessage();
Showmessage();
}
void Readmessage()
{
static int CharCount = 0;
char EnterCheck = '\n';
while (Serial.available() > 0 && Done == false)
{
NextChar = Serial.read();
if (NextChar == CountChar || CountCharLow) // this is what I've tried
{
Counter++;
}
if (NextChar != EnterCheck)
{
Message[CharCount] = NextChar;
CharCount++;
}
else
{
InverseMessage(CharCount);
Message[CharCount-1] = '\0';
CharCount=0;
Done = true;
}
}
}
void InverseMessage(int Characters)
{
for (int InversedCharCount = 0; InversedCharCount < Characters-1; InversedCharCount++)
{
MessageInversed[Characters-2-InversedCharCount] = Message[InversedCharCount];
}
MessageInversed[Characters] = '\0';
}
void Showmessage()
{
if (Done == true)
{
Serial.print("Your message length is: ");
Serial.println(strlen(Message));
Serial.print("This is your message: ");
Serial.println(Message);
Serial.print("Your inversed message length is: ");
Serial.println(strlen(MessageInversed));
Serial.print("Your reversed message is: ");
Serial.println(MessageInversed);
if (Counter == 1)
{
Serial.print("The letter E has been typed ");
Serial.print(Counter);
Serial.println(" time");
}
else
{
Serial.print("The letter E has been typed a total of ");
Serial.print(Counter);
Serial.println(" times");
}
Serial.print("<Please enter next text>");
Done = false;
Counter = 0;
}
}
您需要檢查 NextChar 是否是小寫字母,因此它應該可以通過鍵入:
if(NextChar == CountChar || NextChar == CountCharLow) {...}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.