CharacterTypes (C++)

From RAD Studio Code Examples
Jump to: navigation, search

Description

The following example demostrates the use of some routines in Character unit that allow to determine the type of a character. When the user types something in the first memo, the second one gets filled with relevant information.

Code

void __fastcall TMainForm::mmTextChange(TObject *Sender)
{
	String allText = mmText->Text;

	int LDigits = 0;
	int LLetters = 0;
	int LLower = 0;
	int LUpper = 0;
	int LNumber = 0;
	int LPuct = 0;
	int LSep = 0;
	int LSymbols = 0;
	int LWhites = 0;

	/* Calculate all all kinds of charcters in the memo */
	for (int i = 1; i <= allText.Length(); ++i)
	{
		/* Check for digit */
		if (IsDigit(allText[i])) LDigits++;

		/* Check for number */
		if (IsNumber(allText[i])) LNumber++;

		/* Check for letter */
		if (IsLetter(allText[i])) LLetters++;

		/* Check for lower-cased letter */
		if (IsLower(allText[i])) LLower++;

		/* Check for upper-cased letter */
		if (IsUpper(allText[i])) LUpper++;

		/* Check for punctuation */
		if (IsPunctuation(allText[i])) LPuct++;

		/* Check for separators */
		if (IsSeparator(allText[i])) LSep++;

		/* Check for symbols */
		if (IsSymbol(allText[i])) LSymbols++;

		/* Check for symbols */
		if (IsWhiteSpace(allText[i])) LWhites++;
	}

  mmRes->Text = Format(String("%d digits; %d numbers; %d letters; ") +
	"(%d upper and %d lower); %d puctuation; %d separators; " +
	"%d symbols; %d whitespaces",
	ARRAYOFCONST((LDigits, LNumber, LLetters, LUpper, LLower, LPuct,
	 LSep, LSymbols, LWhites))
  );
}

Uses