【问题标题】:Making lettercheck not case sensitive使 lettercheck 不区分大小写
【发布时间】:2020-06-18 07:06:06
【问题描述】:

所以我需要让这段代码能够计算指定的字母(在本例中为字母 E)。我已经做到了计算在句子中输入了多少次字母的程度,但是它区分大小写。现在它只计算大写 E,可以更改,但我不知道它如何读取大写和小写。

我找到了equalsIgnoreCase() 代码,但是我不熟悉它,所以我不知道把它放在哪里,它是如何工作的,或者它是否会起作用。就我而言,我尝试了myString.equalsIgnoreCase(MyString2),我相信CharCountCharCountLow

这是我的代码:

//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;
  }
}

【问题讨论】:

  • 我也尝试了 CountChar == CountCharLow,这就是为什么它没有按照我在上面的代码中想要的那样 100% 工作。
  • NextChar == CountChar || CountCharLow 你们从哪里得到这种语法?这不是您将变量与多个值进行比较的方式。你这周至少是第四个,我见过这样做的。

标签: c++ arduino char case-insensitive


【解决方案1】:

您需要检查 NextChar 是否为小写字母,因此它应该可以通过键入:

if(NextChar == CountChar || NextChar == CountCharLow) {...}

【讨论】:

    猜你喜欢
    • 2012-12-01
    • 2013-03-06
    • 2020-02-18
    • 1970-01-01
    • 1970-01-01
    • 2020-06-03
    • 1970-01-01
    • 2011-10-14
    • 1970-01-01
    相关资源
    最近更新 更多