Theme
Primary Color
Lesson 16

Check If First Letter Is Capital

محتوى الدرس

#include <iostream>
using namespace std;

/*
  Is First Letter Capital
  "Hello Elzero Web School" -> True -> All Caps
  "Hello elzero Web School" -> False -> elzero -> [e] Small
*/

bool firstCapital(string st)
{
  bool allCaps = true;

  for (int i = 0; i < st.length(); i++)
  {
    if (i == 0 && !(st[i] >= 'A' && st[i] <= 'Z'))
    {
      allCaps = false;
    }
    else if (st[i] == ' ' && !(st[i + 1] >= 'A' && st[i + 1] <= 'Z'))
    {
      allCaps = false;
    }
    if (!allCaps)
    {
      return false;
    }
  }

  return allCaps;
}

int main()
{
  cout << firstCapital("Hello Elzero Web School") << endl;
  cout << firstCapital("Hello Elzero web") << endl;
  cout << firstCapital("HWWllo Elzero Web") << endl;
  return 0;
}