# ctype

***

**1. Checking Character Type**

```cpp
#include <ctype.h>

int main() {
  char c = 'a';
  if (isalpha(c)) {
    printf("Character %c is an alphabet.\n", c);
  } else if (isdigit(c)) {
    printf("Character %c is a digit.\n", c);
  } else if (isspace(c)) {
    printf("Character %c is a whitespace.\n", c);
  }
  return 0;
}
```

**2. Converting Character Case**

```cpp
#include <ctype.h>

int main() {
  char c = 'a';
  c = toupper(c);  // Convert 'a' to 'A'
  printf("Uppercase character: %c\n", c);
  return 0;
}
```

**3. Removing Punctuation**

```cpp
#include <ctype.h>

int main() {
  char str[] = "Hello, world!";
  for (int i = 0; str[i]; i++) {
    if (ispunct(str[i])) {
      str[i] = ' ';  // Replace punctuation with whitespace
    }
  }
  printf("Punctuation removed: %s\n", str);
  return 0;
}
```

**4. Extracting Integers from a String**

```cpp
#include <ctype.h>

int main() {
  char str[] = "1234";
  int num = 0;
  for (int i = 0; str[i]; i++) {
    if (isdigit(str[i])) {
      num = num * 10 + str[i] - '0';  // Convert digit to integer
    }
  }
  printf("Extracted integer: %d\n", num);
  return 0;
}
```

**5. Tokenizing a String**

```cpp
#include <ctype.h>

int main() {
  char str[] = "Hello world";
  char *token = strtok(str, " ");  // Tokenize on whitespace
  while (token) {
    printf("Token: %s\n", token);
    token = strtok(NULL, " ");  // Get next token
  }
  return 0;
}
```

**6. Ignoring Case in String Comparisons**

```cpp
#include <ctype.h>
#include <string.h>

int main() {
  char str1[] = "Hello";
  char str2[] = "hello";
  int result = strcasecmp(str1, str2);
  if (result == 0) {
    printf("Strings are equal (case-insensitive).\n");
  } else {
    printf("Strings are not equal (case-insensitive).\n");
  }
  return 0;
}
```

**7. Validating Email Addresses**

```cpp
#include <ctype.h>

int main() {
  char email[] = "example@domain.com";
  int valid = 1;
  for (int i = 0; email[i]; i++) {
    if (!isalnum(email[i]) && email[i] != '.' && email[i] != '@') {
      valid = 0;  // Invalid email address
      break;
    }
  }
  if (valid) {
    printf("Email address is valid.\n");
  } else {
    printf("Email address is invalid.\n");
  }
  return 0;
}
```

**8. Encrypting Text using Caesar Cipher**

```cpp
#include <ctype.h>

int main() {
  char plainText[] = "Hello";
  int shift = 3;
  char cipherText[strlen(plainText) + 1];
  for (int i = 0; plainText[i]; i++) {
    if (isalpha(plainText[i])) {
      cipherText[i] = (
          isupper(plainText[i]) ? 'A' : 'a') +
          ((plainText[i] - (isupper(plainText[i]) ? 'A' : 'a') + shift) % 26);
    } else {
      cipherText[i] = plainText[i];
    }
  }
  cipherText[strlen(plainText)] = '\0';
  printf("Cipher text: %s\n", cipherText);
  return 0;
}
```

**9. Checking for Numeric Input**

```cpp
#include <ctype.h>

int main() {
  char input[100];
  printf("Enter a number: ");
  scanf("%s", input);
  int valid = 1;
  for (int i = 0; input[i]; i++) {
    if (!isdigit(input[i])) {
      valid = 0;  // Invalid numeric input
      break;
    }
  }
  if (valid) {
    printf("Input is a valid number.\n");
  } else {
    printf("Input is not a valid number.\n");
  }
  return 0;
}
```

**10. Removing Duplicate Characters**

```cpp
#include <ctype.h>

int main() {
  char str[] = "Hello world";
  char uniqueStr[strlen(str) + 1];
  int i = 0, j = 0;
  while (str[i]) {
    if (j == 0 || str[i] != uniqueStr[j - 1]) {
      uniqueStr[j++] = str[i];
    }
    i++;
  }
  uniqueStr[j] = '\0';
  printf("Unique string: %s\n", uniqueStr);
  return 0;
}
```

**11. Counting Words in a String**

```cpp
#include <ctype.h>

int main() {
  char str[] = "Hello world";
  int wordCount = 0;
  int inWord = 0;
  for (int i = 0; str[i]; i++) {
    if (isalpha(str[i])) {
      if (!inWord) {
        wordCount++;
        inWord = 1;
      }
    } else {
      inWord = 0;
    }
  }
  printf("Word count: %d\n", wordCount);
  return 0;
}
```

**12. Finding the Length of a String without Using strlen**

```cpp
#include <ctype.h>

int main() {
  char str[] = "Hello world";
  int length = 0;
  while (str[length]) {
    length++;
  }
  printf("String length: %d\n", length);
  return 0;
}
```

**13. Reversing a String**

```cpp
#include <ctype.h>

int main() {
  char str[] = "Hello world";
  int len = strlen(str);
  for (int i = 0, j = len - 1; i < j; i++, j--) {
    char temp = str[i];
    str[i] = str[j];
    str[j] = temp;
  }
  printf("Reversed string: %s\n", str);
  return 0;
}
```

**14. Converting a Hexadecimal String to an Integer**

```cpp
#include <ctype.h>

int main() {
  char hexString[] = "1A";
  int num = 0;
  for (int i = 0; hexString[i]; i++) {
    if (isdigit(hexString[i])) {
      num = num * 16 + (hexString[i] - '0');
    } else if (isxdigit(hexString[i])) {
      num = num * 16 + (toupper(hexString[i]) - 'A' + 10);
    }
  }
  printf("Hexadecimal integer: %d\n", num);
  return 0;
}
```

**15. Checking for Balanced Parentheses**

```cpp
#include <ctype.h>
#include <stack>

int main() {
  char str[] = "()[]{}";
  std::stack<char> stack;
  for (int i = 0; str[i]; i++) {
    if (str[i] == '(' || str[i] == '[' || str[i] == '{') {
      stack.push(str[i]);
    } else if (str[i] == ')' || str[i] == ']' || str[i] == '}') {
      if (stack.empty() || stack.top() != str[i] - 1 && stack.top() != str[i] - 2) {
        printf("Parentheses are not balanced.\n");
        return 0;
      }
      stack.pop();
    }
  }
  if (stack.empty()) {
    printf("Parentheses are balanced.\n");
  } else {
    printf("Parentheses are not balanced.\n");
  }
  return 0;
}
```

**16. Counting Occurrences of a Character**

```cpp
#include <ctype.h>

int main() {
  char str[] = "Hello world";
  char target = 'o';
  int count = 0;
  for (int i = 0; str[i]; i++) {
    if (tolower(str[i]) == tolower(target)) {
      count++;
    }
  }
  printf("Occurrences of '%c': %d\n", target, count);
  return 0;
}
```

**17. Removing Leading and Trailing Whitespace**

```cpp
#include <ctype.h>

int main() {
  char str[] = "  Hello world  ";
  int start = 0, end = strlen(str) - 1;
  while (isspace(str[start])) {
    start++;
  }
  while (isspace(str[end])) {
    end--;
  }
  str[end + 1] = '\0';  // Truncate the string after the last non-whitespace character
  printf("Trimmed string: %s\n", str);
  return 0;
}
```

**18. Capitalizing the First Letter of a String**

```cpp
#include <ctype.h>

int main() {
  char str[] = "hello world";
  str[0] = toupper(str[0]);  // Capitalize the first letter
  printf("Capitalized string: %s\n", str);
  return 0;
}
```

**19. Finding the Minimum and Maximum Values in a String**

```cpp
#include <ctype.h>

int main() {
  char str[] = "12345";
  int min = str[0], max = str[0];
  for (int i = 1; str[i]; i++) {
    if (isdigit(str[i])) {
      min = min < str[i] ? min : str[i];
      max = max > str[i] ? max : str[i];
    }
  }
  printf("Minimum value: %c\n", min);
  printf("Maximum value: %c\n", max);
  return 0;
}
```

**20. Finding the Most Frequently Occurring Character**

```cpp
#include <ctype.h>

int main() {
  char str[] = "Hello world";
  int freq[256] = {0};
  for (int i = 0; str[i]; i++) {
    freq[tolower(str[i])]++;
  }
  int maxFreq = 0, maxChar = 0;
  for (int i = 0; i < 256; i++) {
    if (freq[i] > maxFreq) {
      maxFreq = freq[i];
      maxChar = i;
    }
  }
  printf("Most frequently occurring character: '%c' (count: %d)\n", maxChar, maxFreq);
  return 0;
}
```

**21. Removing Consecutive Duplicates**

```cpp
#include <ctype.h>

int main() {
  char str[] = "Hello world";
  char uniqueStr[strlen(str) + 1];
  int i = 0, j = 0;
  while (str[i]) {
    if (i == 0 || str[i] != str[i - 1]) {
      uniqueStr[j++] = str[i];
    }
    i++;
  }
  uniqueStr[j] = '\0';
  printf("Unique string: %s\n", uniqueStr);
  return 0;
}
```

**22. Finding the Position of the Last Occurrence of a Character**

```cpp
#include <ctype.h>

int main() {
  char str[] = "Hello world";
  char target = 'l';
  int lastPosition = -1;
  for (int i = 0; str[i]; i++) {
    if (tolower(str[i]) == tolower(target)) {
      lastPosition = i;
    }
  }
  printf("Last position of '%c': %d\n", target, lastPosition);
  return 0;
}
```

**23. Converting a String to Uppercase**

```cpp
#include <ctype.h>

int main() {
  char str[] = "hello world";
  for (int i = 0; str[i]; i++) {
    str[i] = toupper(str[i]);
  }
  printf("Uppercase string: %s\n", str);
  return 0;
}
```

**24. Converting a String to Lowercase**

```cpp
#include <ctype.h>

int main() {
  char str[] = "HELLO WORLD";
  for (int i = 0; str[i]; i++) {
    str[i] = tolower(str[i]);
  }
  printf("Lowercase string: %s\n", str);
  return 0;
}
```

**25. Counting the Number of Vowels in a String**

```cpp
#include <ctype.h>

int main() {
  char str[] = "Hello world";
  int vowelCount = 0;
  for (int i = 0; str[i]; i++) {
    char c = tolower(str[i]);
    if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
      vowelCount++;
    }
  }
  printf("Number of vowels: %d\n", vowelCount);
  return 0;
}
```

**26. Validating a Password**

```cpp
#include <ctype.h>

int main() {
  char password[] = "StrongPassword123";
  int valid = 1;
  int length = strlen(password);
  if (length < 8) {
    valid = 0;  // Password must be at least 8 characters long
  }
  int hasUppercase = 0, hasLowercase = 0, hasDigit = 0, hasSymbol = 0;
  for (int i = 0; i < length; i++) {
    char c = password[i];
    if (isupper(c)) {
      hasUppercase = 1;
    } else if (islower(c)) {
      hasLowercase = 1;
    } else if (isdigit(c)) {
      hasDigit = 1;
    } else {
      hasSymbol = 1;
    }
  }
  if (!hasUppercase || !hasLowercase || !hasDigit || !hasSymbol) {
    valid = 0;  // Password must contain uppercase, lowercase, digits, and symbols
  }
  if (valid) {
    printf("Password is valid.\n");
  } else {
    printf("Password is invalid.\n");
  }
  return 0;
}
```

**27. Finding the Length of the Longest Word**

```cpp
#include <ctype.h>

int main() {
  char str[] = "Hello world, how are you?";
  int maxLen = 0;
  int curLen = 0;
  for (int i = 0; str[i]; i++) {
    if (isalpha(str[i])) {
      curLen++;
    } else {
      if (curLen > maxLen) {
        maxLen = curLen;
      }
      curLen = 0;
    }
  }
  if (curLen > maxLen) {
    maxLen = curLen;
  }
  printf("Length of the longest word: %d\n", maxLen);
  return 0;
}
```

**28. Normalizing a String (Removing Punctuation, Extra Whitespace)**

```cpp
#include <ctype.h>

int main() {
  char str[] = "Hello, world!  How are you?";
  char normalizedStr[strlen(str) + 1];
  int i = 0, j = 0;
  while (str[i]) {
    char c = str[i++];
    if (!ispunct(c) && !isspace(c) || (isspace(c) && !isspace(str[i]))) {
      normalizedStr[j++] = c;
    }
  }
  normalizedStr[j] = '\0';
  printf("Normalized string: %s\n", normalizedStr);
  return 0;
}
```

**29. Finding the Common Prefix of Two Strings**

```cpp
#include <ctype.h>

int main() {
  char str1[] = "Hello";
  char str2[] = "HelloWorld";
  int prefixLen = 0;
  while (str1[prefixLen] && str2[prefixLen] && str1[prefixLen] == str2[prefixLen]) {
    prefixLen++;
  }
  printf("Common prefix: %.*s\n", prefixLen, str1);
  return 0;
}
```

**30. Truncating a String to a Specified Length**

```cpp
#include <ctype.h>

int main() {
  char str[] = "Hello world";
  int maxLength = 6;
  char truncatedStr[maxLength + 1];
  strncpy(truncatedStr, str, maxLength);
  truncatedStr[maxLength] = '\0';
  printf("Truncated string: %s\n", truncatedStr);
  return 0;
}
```

**31. Generating a Random String**

```cpp
#include <ctype.h>
#include <time.h>

int main() {
  srand(time(0));
  int length = 10;
  char randomStr[length + 1];
  for (int i = 0; i < length; i++) {
    randomStr[i] = (char)(rand() % 26 + 'a');
  }
  randomStr[length] = '\0';
  printf("Random string: %s\n", randomStr);
  return 0;
}
```

**32. Comparing Strings Case-Insensitive**

```cpp
#include <ctype.h>

int main() {
  char str1[] = "Hello";
  char str2[] = "HeLlO";
  int result = strcasecmp(str1, str2);
  if (result == 0) {
    printf("Strings are equal (case-insensitive).\n");
  } else {
    printf("Strings are not equal (case-insensitive).\n");
  }
  return 0;
}
```

**33. Replacing Occurrences of a Character**

```cpp
#include <ctype.h>

int main() {
  char str[] = "Hello world";
  char target = 'o';
  char replacement = 'a';
  for (int i = 0; str[i]; i++) {
    if (tolower(str[i]) == tolower(target)) {
      str[i] = replacement;
    }
  }
  printf("Replaced string: %s\n", str);
  return 0;
}
```

**34. Encrypting Text using XOR Cipher**

```cpp
#include <ctype.h>

int main() {
  char plainText[] = "Hello";
  char key = 'X';
  char cipherText[strlen(plainText) + 1];
  for (int i = 0; plainText[i]; i++) {
    cipherText[i] = plainText[i] ^ key;
  }
  cipherText[strlen(plainText)] = '\0';
  printf("Cipher text: %s\n", cipherText);
  return 0;
}
```

**35. Decrypting Text Encrypted by XOR Cipher**

```cpp
#include <ctype.h>

int main() {
  char cipherText[] = "XWlol";
  char key = 'X';
  char plainText[strlen(cipherText) + 1];
  for (int i = 0; cipherText[i]; i++) {
    plainText[i] = cipherText[i] ^ key;
  }
  plainText[strlen(cipherText)] = '\0';
  printf("Plain text: %s\n", plainText);
  return 0;
}
```

**36. Converting a Number to a Roman Numeral**

```cpp
#include <ctype.h>

int main() {
  int num = 1994;
  char romanNumeral[50];  // Assuming the maximum length of Roman representation is 50
  int i = 0;
  while (num > 0) {
    if (num >= 1000) {
      romanNumeral[i++] = 'M';
      num -= 1000;
    } else if (num >= 900) {
      romanNumeral[i++] = 'C';
      romanNumeral[i++] = 'M';
      num -= 900;
    } else if (num >= 500) {
      romanNumeral[i++] = 'D';
      num -= 500;
    } else if (num >= 400) {
      romanNumeral[i++] = 'C';
      romanNumeral[i++] = 'D';
      num -= 400;
    } else if (num >= 100) {
      romanNumeral[i++] = 'C';
      num -= 100;
    } else if (num >= 90) {
      romanNumeral[i++] = 'X';
      romanNumeral[i++] = 'C';
      num -= 90;
    } else if (num >= 50) {
      romanNumeral[i++] = 'L';
      num -= 50;
    } else if (num >= 40) {
      romanNumeral[i++] = 'X';
      romanNumeral[i++] = 'L';
      num -= 40;
    } else if (num >= 10) {
      romanNumeral[i++] = 'X';
      num -= 10;
    } else if (num >= 9) {
      romanNumeral[i++] = 'I';
      romanNumeral[i++] = 'X';
      num -= 9;
    } else if (num >= 5) {
      romanNumeral[i++] = 'V';
      num -= 5;
    } else if (num >= 4) {
      romanNumeral[i++] = 'I';
      romanNumeral[i++] = 'V';
      num -= 4;
    } else if (num >= 1) {
      romanNumeral[i++] = 'I';
      num -= 1;
    }
  }
  romanNumeral[i] = '\0';
  printf("Roman numeral: %s\n", romanNumeral);
  return 0;
}
```

**37. Converting a Roman Numeral to a Number**

```cpp
#include <ctype.h>

int main() {
  char romanNumeral[] = "MCMXCIV";
  int num = 0;
  int i = 0;
  while (romanNumeral[i]) {
    char c = toupper(romanNumeral[i]);
    switch (c) {
      case 'M':
        num += 1000;
        break;
      case 'C':
        if (romanNumeral[i + 1] == 'M') {
          num += 900;
          i++;
        } else if (romanNumeral[i + 1] == 'D') {
          num += 400;
          i++;
        } else {
          num += 100;
        }
        break;
      case 'D':
        num += 500;
        break;
      case 'X':
        if (romanNumeral[i + 1] == 'C') {
          num += 90;
          i++;
        } else if (romanNumeral[i + 1] == 'L') {
          num += 40;
          i++;
        } else {
          num += 10;
        }
        break;
      case 'L':
        num += 50;
        break;
      case 'V':
        num += 5;
        break;
      case 'I':
        if (romanNumeral[i + 1] == 'V') {
          num += 4;
          i++;
        } else if (romanNumeral[i + 1] == 'X') {
          num += 9;
          i++;
        } else {
          num += 1;
        }
        break;
    }
    i++;
  }
  printf("Number: %d\n", num);
  return 0;
}
```

**38. Finding the ASCII Value of a Character**

```cpp
#include <ctype.h>

int main() {
  char c = 'A';
  int asciiValue = c;
  printf("ASCII value of '%c': %d\n", c, asciiValue);
  return 0;
}
```

**39. Converting a Character to its ASCII Code**

```cpp
#include <ctype.h>

int main() {
  int asciiCode = 65;
  char c = asciiCode;
  printf("Character for ASCII code %d: '%c'\n", asciiCode, c);
  return 0;
}
```

**40. Checking if a String is a Palindrome**

```cpp
#include <ctype.h>

int main() {
  char str[] = "racecar";
  int len = strlen(str);
  int palindrome = 1;
  for (int i = 0; i < len / 2; i++) {
    if (tolower(str[i]) != tolower(str[len - i - 1])) {
      palindrome = 0;
      break;
    }
  }
  if (palindrome) {
    printf("The string is a palindrome.\n");
  } else {
    printf("The string is not a palindrome.\n");
  }
  return 0;
}
```

**41. Finding the First Non-Repeating Character**

```cpp
#include <ctype.h>

int main() {
  char str[] = "Hello world";
  int freq[256] = {0};
  for (int i = 0; str[i]; i++) {
    freq[tolower(str[i])]++;
  }
  int nonRepeating = -1;
  for (int i = 0; i < 256; i++) {
    if (freq[i] == 1) {
      nonRepeating = i;
      break;
    }
  }
  if (nonRepeating == -1) {
    printf("No non-repeating character found.\n");
  } else {
    printf("First non-repeating character: '%c'\n", nonRepeating);
  }
  return 0;
}
```

**42. Checking if a String Contains All Unique Characters**

```cpp
#include <ctype.h>

int main() {
  char str[] = "Hello world";
  int unique = 1;
  int freq[256] = {0};
  for (int i = 0; str[i]; i++) {
    freq[tolower(str[i])]++;
  }
  for (int i = 0; i < 256; i++) {
    if (freq[i] > 1) {
      unique = 0;
      break;
    }
  }
  if (unique) {
    printf("The string contains all unique characters.\n");
  } else {
    printf("The string does not contain all unique characters.\n");
  }
  return 0;
}
```

**43. Converting a String to Camel Case**

```cpp
#include <ctype.h>

int main() {
  char str[] = "Hello World";
  int i = 0;
  while (str[i]) {
    if (i == 0 || str[i - 1] == ' ') {
      str[i] = toupper(str[i]);
    } else {
      str[i] = tolower(str[i]);
    }
    i++;
  }
  printf("Camel case string: %s\n", str);
  return 0;
}
```

**44. Finding the Number of Words in a String Using isspace**

```cpp
#include <ctype.h>

int main() {
  char str[] = "Hello world, how are you?";
  int wordCount = 0;
  int inWord = 0;
  for (int i = 0; str[i]; i++) {
    if (isspace(str[i])) {
      inWord = 0;
    } else if (!inWord) {
      wordCount++;
      inWord = 1;
    }
  }
  printf("Word count: %d\n", wordCount);
  return 0;
}
```

**45. Checking if a Character is a Hexadecimal Digit**

```cpp
#include <ctype.h>

int main() {
  char c = 'A';
  int isHex = isxdigit(c);
  if (isHex) {
    printf("Character '%c' is a hexadecimal digit.\n", c);
  } else {
    printf("Character '%c' is not a hexadecimal digit.\n", c);
  }
  return 0;
}
```

**46. Converting a Character to its Lowercase Equivalent**

```cpp
#include <ctype.h>

int main() {
  char c = 'A';
  c = tolower(c);
  printf("Lowercase equivalent of '%c': '%c'\n", c);
  return 0;
}
```

**47. Checking if a String is in Title Case**

```cpp
#include <ctype.h>

int main() {
  char str[] = "Hello World";
  int titleCase = 1;
  for (int i = 0; str[i]; i++) {
    if (i == 0 || str[i - 1] == ' ') {
      if (!isupper(str[i])) {
        titleCase = 0;
        break;
      }
    } else {
      if (!islower(str[i])) {
        titleCase = 0;
        break;
      }
    }
  }
  if (titleCase) {
    printf("The string is in title case.\n");
  } else {
    printf("The string is not in title case.\n");
  }
  return 0;
}
```

**48. Finding the Number of Different Characters in a String**

```cpp
#include <ctype.h>

int main() {
  char str[] = "Hello world";
  int uniqueChars = 0;
  int freq[256] = {0};
  for (int i = 0; str[i]; i++) {
    freq[tolower(str[i])]++;
  }
  for (int i = 0; i < 256; i++) {
    if (freq[i] > 0) {
      uniqueChars++;
    }
  }
  printf("Number of different characters: %d\n", uniqueChars);
  return 0;
}
```

**49. Creating a Histogram of Characters in a String**

```cpp
#include <ctype.h>

int main() {
  char str[] = "Hello world";
  int freq[256] = {0};
  for (int i = 0; str[i]; i++) {
    freq[tolower(str[i])]++;
  }
  for (int i = 0; i < 256; i++) {
    if (freq[i] > 0) {
      printf("%c: %d\n", i, freq[i]);
    }
  }
  return 0;
}
```

**50. Comparing if Two Strings are Anagrams**

```cpp
#include <ctype.h>

int main() {
  char str1[] = "hello";
  char str2[] = "olleh";
  int anagrams = 1;
  int freq1[256] = {0};
  int freq2[256] = {0};
  for (int i = 0; str1[i]; i++) {
    freq1[tolower(str1[i])]++;
  }
  for (int i = 0; str2[i]; i++) {
    freq2[tolower(str2[i])]++;
  }
  for (int i = 0; i < 256; i++) {
    if (freq1[i] != freq2[i]) {
      anagrams = 0;
      break;
    }
  }
  if (anagrams) {
    printf("The strings are anagrams.\n");
  } else {
    printf("The strings are not anagrams.\n");
  }
  return 0;
}
```
