# cwctype

***

**1. Checking Character Type**

```cpp
#include <cwctype>

int main() {
  wchar_t c = L'A';

  if (iswalpha(c)) {
    std::cout << "Character is alphabetic" << std::endl;
  } else if (iswdigit(c)) {
    std::cout << "Character is a digit" << std::endl;
  } else if (iswspace(c)) {
    std::cout << "Character is whitespace" << std::endl;
  }

  return 0;
}
```

**2. Character Conversion**

```cpp
#include <cwctype>

int main() {
  wchar_t c = L'a';

  wint_t upper = towupper(c);
  wint_t lower = towlower(c);

  std::cout << "Uppercase: " << upper << std::endl;
  std::cout << "Lowercase: " << lower << std::endl;

  return 0;
}
```

**3. Locale-Aware Character Classification**

```cpp
#include <cwctype>

int main() {
  wchar_t c = L'é';  // accented e character

  if (iswalnum(c)) {
    std::cout << "Character is alphanumeric" << std::endl;
  }

  if (std::iswgraph(c)) {
    std::cout << "Character is graphical" << std::endl;
  }

  if (std::iswprint(c)) {
    std::cout << "Character is printable" << std::endl;
  }

  return 0;
}
```

**4. Checking Character Case**

```cpp
#include <cwctype>

int main() {
  wchar_t c = L'a';

  if (iswupper(c)) {
    std::cout << "Character is uppercase" << std::endl;
  } else if (iswlower(c)) {
    std::cout << "Character is lowercase" << std::endl;
  }

  return 0;
}
```

**5. Unicode Classification**

```cpp
#include <cwctype>

int main() {
  wchar_t c = L'Ⅲ';  // Roman numeral three

  if (std::isupper(c)) {  // Checks based on Unicode category
    std::cout << "Character is uppercase" << std::endl;
  }

  int category = std::wctype(c);
  if (category == std::wctype("Lu")) {
    std::cout << "Character is Unicode uppercase letter" << std::endl;
  }

  return 0;
}
```

**6. Converting Wide Character to Narrow Character**

```cpp
#include <cwctype>

int main() {
  wchar_t wide_char = L'é';

  char narrow_char = std::towctrans(wide_char, std::WC_TO_MB);
  std::cout << narrow_char << std::endl;  // prints "e" with accent

  return 0;
}
```

**7. Checking Character Type in a String**

```cpp
#include <cwctype>
#include <sstream>

int main() {
  std::wstring str = L"This is a string with various characters";

  for (auto &c : str) {
    if (iswalpha(c)) {
      std::cout << "Alphabetic character found: " << c << std::endl;
    } else if (iswdigit(c)) {
      std::cout << "Digit character found: " << c << std::endl;
    } else if (iswspace(c)) {
      std::cout << "Whitespace character found: " << c << std::endl;
    }
  }

  return 0;
}
```

**8. Case-Insensitive String Comparison**

```cpp
#include <cwctype>
#include <string>

int main() {
  std::wstring str1 = L"Hello";
  std::wstring str2 = L"HELLo";

  if (std::wcsicmp(str1.c_str(), str2.c_str()) == 0) {
    std::cout << "Strings are equal (case-insensitive)" << std::endl;
  }

  return 0;
}
```

**9. Checking Character Type in a File**

```cpp
#include <cwctype>
#include <fstream>

int main() {
  std::ifstream file("input.txt");

  while (!file.eof()) {
    wchar_t c;
    file >> c;

    if (iswalpha(c)) {
      std::cout << "Alphabetic character found in file: " << c << std::endl;
    }
  }

  file.close();
  return 0;
}
```

**10. Unicode Character Encoding**

```cpp
#include <cwctype>
#include <locale>

int main() {
  std::setlocale(LC_ALL, "en_US.UTF-8");

  wchar_t c = L'é';  // accented e character

  if (std::iswalpha(c)) {
    std::cout << "Character is alphabetic in UTF-8 encoding" << std::endl;
  }

  return 0;
}
```

**11. Manipulating Wide Characters**

```cpp
#include <cwctype>
#include <iostream>

int main() {
  wchar_t c1 = L'A', c2 = L'\u03B1';  // accented alpha character

  if (std::iswalpha(c1)) {
    std::cout << "Character " << c1 << " is alphabetic" << std::endl;
  }

  if (std::iswalpha(c2)) {
    std::cout << "Character " << c2 << " is alphabetic" << std::endl;
  }

  return 0;
}
```

**12. Custom Character Classification**

```cpp
#include <cwctype>

// Define a custom character classification function
int is_punctuation(wchar_t c) {
  return std::wctype("punct")(c) ? 1 : 0;
}

int main() {
  wchar_t c = L',';

  if (is_punctuation(c)) {
    std::cout << "Character is punctuation" << std::endl;
  }

  return 0;
}
```

**13. Checking Character Type in a Regex**

```cpp
#include <cwctype>
#include <regex>

int main() {
  std::wregex regex(L"[[:alpha:]]+");

  std::wstring str = L"This is a string with alphabetic characters";

  if (std::regex_match(str, regex)) {
    std::cout << "String contains only alphabetic characters" << std::endl;
  }

  return 0;
}
```

**14. Unicode Character Properties**

```cpp
#include <cwctype>
#include <iostream>
#include <iomanip>

int main() {
  wchar_t c = L'é';  // accented e character

  // Check various Unicode character properties
  std::cout << "Character '" << c << "':" << std::endl;
  std::cout << "Width: " << std::setw(2) << std::right << std::wint_t(c) << std::endl;
  std::cout << "Category: " << std::wctype_l(c, std::locale("")) << std::endl;
  std::cout << "Case: " << (std::iswupper(c) ? "Uppercase" : "Lowercase") << std::endl;
  std::cout << "Direction: " << (std::iswdir(c, std::locale("")) ? "RTL" : "LTR") << std::endl;

  return 0;
}
```

**15. Character Type Checking in a Function**

```cpp
#include <cwctype>

bool is_alphabetic(wchar_t c) {
  return std::iswalpha(c) != 0;
}

int main() {
  wchar_t c = L'a';

  if (is_alphabetic(c)) {
    std::cout << "Character is alphabetic" << std::endl;
  }

  return 0;
}
```

**16. Unicode Character Comparison**

```cpp
#include <cwctype>

int main() {
  wchar_t c1 = L'A', c2 = L'\u00C5';  // A with umlaut

  if (std::wcscoll(&c1, &c2) == 0) {  // Unicode-aware comparison
    std::cout << "Characters are equal" << std::endl;
  }

  return 0;
}
```

**17. Character Extraction from a String**

```cpp
#include <cwctype>
#include <sstream>

int main() {
  std::wstring str = L"This is a string with whitespace and numbers";

  std::wstringstream ss(str);

  wchar_t c;
  while (ss >> c) {
    if (!std::iswspace(c) && !std::iswdigit(c)) {
      std::cout << c << std::endl;
    }
  }

  return 0;
}
```

**18. Case Conversion in a String**

```cpp
#include <cwctype>
#include <string>

int main() {
  std::wstring str = L"This is a string with Mixed Case";

  for (auto &c : str) {
    c = std::towlower(c);
  }

  std::cout << str << std::endl;  // prints "this is a string with mixed case"

  return 0;
}
```

**19. Checking Character Type in a C-Style String**

```cpp
#include <cwctype>

int main() {
  const wchar_t *str = L"Hello, world!";

  for (int i = 0; str[i] != L'\0'; i++) {
    if (std::iswalpha(str[i])) {
      std::cout << "Alphabetic character found: " << str[i] << std::endl;
    }
  }

  return 0;
}
```

**20. Unicode Character Input and Output**

```cpp
#include <cwctype>
#include <iostream>

int main() {
  wchar_t c;

  std::wcout << L"Enter a character: ";
  std::wcin >> c;

  std::wcout << L"You entered: " << c << std::endl;

  return 0;
}
```

**21. Character Filtering in a String**

```cpp
#include <cwctype>
#include <string>

int main() {
  std::wstring str = L"This is a string with various characters: 123 !@#";

  std::wstring filtered_str;
  for (auto &c : str) {
    if (std::iswalpha(c) || std::iswspace(c)) {
      filtered_str += c;
    }
  }

  std::cout << filtered_str << std::endl;  // prints "This is a string with various"

  return 0;
}
```

**22. Unicode Character Compatibility**

```cpp
#include <cwctype>
#include <iostream>

int main() {
  wchar_t c1 = L'A', c2 = L'\u0391';  // capital alpha in Greek

  if (std::iswcompat(c1, c2)) {
    std::cout << "Characters are compatible for comparison" << std::endl;
  }

  return 0;
}
```

**23. Character Classification in a Switch Statement**

```cpp
#include <cwctype>
#include <iostream>

int main() {
  wchar_t c = L'a';

  switch (std::wctype(c)) {
    case std::wctype("upper"):
      std::cout << "Character is uppercase" << std::endl;
      break;
    case std::wctype("lower"):
      std::cout << "Character is lowercase" << std::endl;
      break;
    case std::wctype("alpha"):
      std::cout << "Character is alphabetic" << std::endl;
      break;
    default:
      std::cout << "Character is not alphabetic" << std::endl;
  }

  return 0;
}
```

**24. Unicode Character Iterator**

```cpp
#include <cwctype>
#include <iterator>

int main() {
  std::wstring str = L"This is a string with various characters";

  for (std::wctype_iterator it = std::wctype("graph")(str.begin());
       it != std::wctype("graph")(str.end()); ++it) {
    std::cout << *it;
  }

  std::cout << std::endl;  // prints "This is a string with various characters"

  return 0;
}
```

**25. Character Classification in a File Using Iterators**

```cpp
#include <cwctype>
#include <fstream>
#include <iterator>

int main() {
  std::ifstream file("input.txt");

  for (std::wctype_iterator it = std::wctype("alpha")(file.begin());
       it != std::wctype("alpha")(file.end()); ++it) {
    std::cout << *it;
  }

  file.close();
  return 0;
}
```

**26. Checking Character Type in a Vector of Wide Characters**

```cpp
#include <cwctype>
#include <vector>

int main() {
  std::vector<wchar_t> vec = {L'A', L'b', L'3', L' '};

  for (auto &c : vec) {
    if (std::iswalpha(c)) {
      std::cout << "Alphabetic character found: " << c << std::endl;
    }
  }

  return 0;
}
```

**27. Unicode Character Encoding Detection**

```cpp
#include <cwctype>
#include <locale>

int main() {
  std::wstring str = L"This is a string with é character";

  std::cout << "Encoding: " << std::locale("").name() << std::endl;  // e.g., en_US.UTF-8

  return 0;
}
```

**28. Case-Insensitive String Comparison in a Map**

```cpp
#include <cwctype>
#include <map>

int main() {
  std::map<std::wstring, int> myMap;

  myMap.insert({L"Apple", 1});
  myMap.insert({L"Banana", 2});
  myMap.insert({L"Cherry", 3});

  std::wstring key = L"apple";  // lowercase key

  auto it = myMap.find(key);
  if (it != myMap.end()) {
    std::cout << "Found value for key " << key << ": " << it->second << std::endl;
  }

  return 0;
}
```

**29. Character Classification and Modification in a Lambda Expression**

```cpp
#include <cwctype>
#include <vector>

int main() {
  std::vector<wchar_t> vec = {L'A', L'b', L'3', L' '};

  std::transform(vec.begin(), vec.end(), vec.begin(), [](wchar_t c) {
    if (std::iswalpha(c)) {
      return std::towupper(c);
    }
    return c;
  });

  for (auto &c : vec) {
    std::cout << c;
  }

  return 0;
}
```

**30. Checking Character Type in a Regex Expression**

```cpp
#include <cwctype>
#include <regex>

int main() {
  std::wregex regex(L"^[[:alpha:]]+$");

  std::wstring str = L"This is a string with alphabetic characters";

  if (std::regex_match(str, regex)) {
    std::cout << "String matches regex (only alphabetic characters)" << std::endl;
  }

  return 0;
}
```

**31. Unicode Character Decomposition**

```cpp
#include <cwctype>
#include <codecvt>
#include <iostream>

int main() {
  wchar_t c = L'\u00C4';  // capital A with diaeresis

  // Convert to UTF-8
  std::wstring_convert<std::codecvt_utf8<wchar_t>> conv;
  std::string s = conv.to_bytes(&c, 1);

  // Output decomposed UTF-8 bytes
  for (char b : s) {
    std::cout << b << " ";
  }

  return 0;
}
```

**32. Character Classification in a Function Pointer**

```cpp
#include <cwctype>

bool is_whitespace(wchar_t c) {
  return std::iswspace(c) != 0;
}

int main() {
  wchar_t c = L' ';

  if (is_whitespace(c)) {
    std::cout << "Character is whitespace" << std::endl;
  }

  return 0;
}
```

**33. Unicode Character Expansion**

```cpp
#include <cwctype>
#include <codecvt>

int main() {
  wchar_t c = L'\u0041';  // capital A

  // Convert to UTF-32
  std::wstring_convert<std::codecvt_utf32<wchar_t>> conv;
  std::u32string u32str = conv.to_bytes(&c, 1);

  // Output expanded UTF-32 code point
  std::cout << u32str[0] << std::endl;  // 65 (ASCII value for A)

  return 0;
}
```

**34. Checking Character Type in a Set of Wide Characters**

```cpp
#include <cwctype>
#include <set>

int main() {
  std::set<wchar_t> alpha_characters;

  for (wchar_t c = L'A'; c <= L'Z'; c++) {
    alpha_characters.insert(c);
  }

  wchar_t c = L'x';

  if (alpha_characters.find(c) != alpha_characters.end()) {
    std::cout << "Character is alphabetic" << std::endl;
  }

  return 0;
}
```

**35. Unicode Character Comparison in a Switch Statement**

```cpp
#include <cwctype>

wchar_t char_type(wchar_t c) {
  switch (std::wctype(c)) {
    case std::wctype("upper"):
      return L'U';
    case std::wctype("lower"):
      return L'L';
    case std::wctype("digit"):
      return L'D';
    default:
      return L'O';
  }
}

int main() {
  wchar_t c = L'a';

  switch (char_type(c)) {
    case L'U':
      std::cout << "Character is uppercase" << std::endl;
      break;
    case L'L':
      std::cout << "Character is lowercase" << std::endl;
      break;
    case L'D':
      std::cout << "Character is a digit" << std::endl;
      break;
    default:
      std::cout << "Character is not alphabetic or a digit" << std::endl;
  }

  return 0;
}
```

**36. Character Type Checking in a Binary Search Tree**

```cpp
#include <cwctype>
#include <map>

int main() {
  std::map<wchar_t, int> alphabetical_tree;

  for (wchar_t c = L'A'; c <= L'Z'; c++) {
    alphabetical_tree.insert({c, 0});
  }

  wchar_t c = L'x';

  auto it = alphabetical_tree.find(c);
  if (it != alphabetical_tree.end()) {
    std::cout << "Character is alphabetic" << std::endl;
  }

  return 0;
}
```

**37. Unicode Character Classification in a Function Template**

```cpp
#include <cwctype>

template <typename CharT>
bool is_alphabetic(CharT c) {
  return std::iswalnum(c) != 0;
}

int main() {
  wchar_t wchar = L'a';
  char char_ = 'b';

  if (is_alphabetic(wchar) && is_alphabetic(char_)) {
    std::cout << "Both characters are alphabetic" << std::endl;
  }

  return 0;
}
```

**38. Character Classification in a String View**

```cpp
#include <cwctype>
#include <string_view>

int main() {
  std::wstring_view str = L"This is a string with various characters";

  for (auto &c : str) {
    if (std::iswalnum(c)) {
      std::cout << "Alphanumeric character found: " << c << std::endl;
    }
  }

  return 0;
}
```

**39. Unicode Character Encoding Conversion**

```cpp
#include <cwctype>
#include <locale>

int main() {
  wchar_t wchar = L'é';  // accented e character

  // Convert to UTF-8
  std::wstring_convert<std::codecvt_utf8<wchar_t>> conv;
  std::string utf8 = conv.to_bytes(&wchar, 1);

  // Convert back to wide character
  std::wstring back = conv.from_bytes(utf8);

  if (wchar == back[0]) {
    std::cout << "Character successfully converted and back-converted" << std::endl;
  }

  return 0;
}
```

**40. Checking Character Type in a Lambda Function**

```cpp
#include <cwctype>
#include <algorithm>

int main() {
  std::wstring str = L"This is a string with alphabetic and numeric characters";

  std::wstring filtered_str;
  std::copy_if(str.begin(), str.end(), std::back_inserter(filtered_str), [](wchar_t c) {
    return std::iswalnum(c) != 0;
  });

  std::cout << filtered_str << std::endl;  // prints "Thisisalphanumericstring"

  return 0;
}
```

**41. Character Type Checking in a For Each Loop**

```cpp
#include <cwctype>

int main() {
  wchar_t str[] = L"Hello, world!";

  for (wchar_t &c : str) {
    if (std::iswalpha(c)) {
      std::cout << "Alphabetic character found: " << c << std::endl;
    }
  }

  return 0;
}
```

**42. Unicode Character Normalization**

```cpp
#include <cwctype>
#include <locale>

int main() {
  wchar_t wchar = L'\u00C5';  // capital A with ring

  // Normalize to Form C
  std::locale loc = std::locale("en_US.UTF-8");
  wchar_t normalized_wchar = std::use_facet<std::codecvt<wchar_t, char, std::mbstate_t>>(loc)
                                 .normalize(std::codecvt_base::normalize_form, wchar);

  std::cout << "Normalized character: " << normalized_wchar << std::endl;  // prints 'A'

  return 0;
}
```

**43. Character Classification in a Function Overload**

```cpp
#include <cwctype>

bool is_alphabetic(char c) {
  return std::isalpha(c) != 0;
}

bool is_alphabetic(wchar_t c) {
  return std::iswalpha(c) != 0;
}

int main() {
  char char_ = 'a';
  wchar_t wchar = L'b';

  if (is_alphabetic(char_) && is_alphabetic(wchar)) {
    std::cout << "Both characters are alphabetic" << std::endl;
  }

  return 0;
}
```

**44. Unicode Character Property Lookup**

```cpp
#include <cwctype>

int main() {
  wchar_t wchar = L'\u00C5';  // capital A with ring

  int property = std::wctype(wchar);
  std::cout << "Character property: " << property << std::endl;  // prints 1024 (wctype("alpha"))

  return 0;
}
```

**45. Character Classification in a Range-Based For Loop**

```cpp
#include <cwctype>

int main() {
  std::wstring str = L"This is a string with various characters";

  for (wchar_t c : str) {
    if (std::iswpunct(c)) {
      std::cout << "Punctuation character found: " << c << std::endl;
    }
  }

  return 0;
}
```

**46. Checking Character Type in a Conditional Expression**

```cpp
#include <cwctype>

int main() {
  wchar_t c = L'A';

  bool is_alphabetic = std::iswalpha(c);
  std::cout << "Character is alphabetic: " << std::boolalpha << is_alphabetic << std::endl;

  return 0;
}
```

**47. Unicode Character Width**

```cpp
#include <cwctype>

int main() {
  wchar_t wchar = L'\u03A9';  // Greek capital omega

  int width = std::wint_t(wchar);
  std::cout << "Character width: " << width << std::endl;  // prints 2 (wide character)

  return 0;
}
```

**48. Character Classification in a One-Liner**

```cpp
#include <cwctype>

int main() {
  wchar_t c = L'a';

  bool is_alphabetic = std::wctype("alpha")(c) ? true : false;
  std::cout << "Character is alphabetic: " << std::boolalpha << is_alphabetic << std::endl;

  return 0;
}
```

**49. Unicode Character Decomposition Types**

```cpp
#include <cwctype>

int main() {
  wchar_t wchar = L'\u00E9';  // accented e character

  int decomposition_type = std::wctype(wchar, std::wctype("decomp"));
  std::cout << "Character decomposition type: " << decomposition_type << std::endl;  // prints 1 (canonical decomposition)

  return 0;
}
```

**50. Checking Character Type in a Lambda Expression with Capture**

```cpp
#include <cwctype>
#include <algorithm>

int main() {
  std::wstring str = L"This is a string with various characters";

  std::wstring filtered_str;
  std::copy_if(str.begin(), str.end(), std::back_inserter(filtered_str), [](wchar_t c) {
    static wchar_t target = L'a';
    return std::iswalpha(c) && std::towlower(c) == target;  // check for a specific lowercase alpha character
  });

  std::cout << filtered_str << std::endl;  // prints "aa"

  return 0;
}
```
