#include <ctype.h>intmain(){char c ='a';if(isalpha(c)){printf("Character %c is an alphabet.\n", c);}elseif(isdigit(c)){printf("Character %c is a digit.\n", c);}elseif(isspace(c)){printf("Character %c is a whitespace.\n", c);}return0;}
2. Converting Character Case
#include <ctype.h>intmain(){char c ='a'; c =toupper(c);// Convert 'a' to 'A'printf("Uppercase character: %c\n", c);return0;}
3. Removing Punctuation
4. Extracting Integers from a String
5. Tokenizing a String
6. Ignoring Case in String Comparisons
7. Validating Email Addresses
8. Encrypting Text using Caesar Cipher
9. Checking for Numeric Input
10. Removing Duplicate Characters
11. Counting Words in a String
12. Finding the Length of a String without Using strlen
13. Reversing a String
14. Converting a Hexadecimal String to an Integer
15. Checking for Balanced Parentheses
16. Counting Occurrences of a Character
17. Removing Leading and Trailing Whitespace
18. Capitalizing the First Letter of a String
19. Finding the Minimum and Maximum Values in a String
20. Finding the Most Frequently Occurring Character
21. Removing Consecutive Duplicates
22. Finding the Position of the Last Occurrence of a Character
23. Converting a String to Uppercase
24. Converting a String to Lowercase
25. Counting the Number of Vowels in a String
26. Validating a Password
27. Finding the Length of the Longest Word
28. Normalizing a String (Removing Punctuation, Extra Whitespace)
29. Finding the Common Prefix of Two Strings
30. Truncating a String to a Specified Length
31. Generating a Random String
32. Comparing Strings Case-Insensitive
33. Replacing Occurrences of a Character
34. Encrypting Text using XOR Cipher
35. Decrypting Text Encrypted by XOR Cipher
36. Converting a Number to a Roman Numeral
37. Converting a Roman Numeral to a Number
38. Finding the ASCII Value of a Character
39. Converting a Character to its ASCII Code
40. Checking if a String is a Palindrome
41. Finding the First Non-Repeating Character
42. Checking if a String Contains All Unique Characters
43. Converting a String to Camel Case
44. Finding the Number of Words in a String Using isspace
45. Checking if a Character is a Hexadecimal Digit
46. Converting a Character to its Lowercase Equivalent
47. Checking if a String is in Title Case
48. Finding the Number of Different Characters in a String
49. Creating a Histogram of Characters in a String
#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;
}
#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;
}
#include <ctype.h>
int main() {
char str[] = "Hello world";
int length = 0;
while (str[length]) {
length++;
}
printf("String length: %d\n", length);
return 0;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#include <ctype.h>
int main() {
char c = 'A';
int asciiValue = c;
printf("ASCII value of '%c': %d\n", c, asciiValue);
return 0;
}
#include <ctype.h>
int main() {
int asciiCode = 65;
char c = asciiCode;
printf("Character for ASCII code %d: '%c'\n", asciiCode, c);
return 0;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#include <ctype.h>
int main() {
char c = 'A';
c = tolower(c);
printf("Lowercase equivalent of '%c': '%c'\n", c);
return 0;
}
#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;
}
#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;
}
#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;
}
#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;
}