C - Character oriented Library functions

Character-oriented library functions in C are functions that operate on individual characters or strings of characters. These functions are part of the standard C library and are declared in the <ctype.h> and <string.h> header files. They provide various operations on characters, such as character testing, character conversion, and string manipulation. Here are some commonly used character-oriented library functions:

1. Character Testing Functions (in <ctype.h>):
  • 'isalpha': Checks if a character is an alphabetic character (a through z or A through Z).
  • 'isdigit': Checks if a character is a decimal digit (0 through 9).
  • 'isalnum': Checks if a character is an alphanumeric character (alphabetic or numeric).
  • 'islower': Checks if a character is a lowercase alphabetic character.
  • 'isupper': Checks if a character is an uppercase alphabetic character.
  • 'isspace': Checks if a character is a whitespace character (e.g., space, tab, newline).
  • 'isprint': Checks if a character is a printable character (including whitespace and special characters).
  • 'ispunct': Checks if a character is a punctuation character.

Example:


#include <stdio.h>
#include <ctype.h>

int main() {
    char ch = 'A';
    if (isalpha(ch)) {
        printf("%c is an alphabetic character.\n", ch);
    }
    return 0;
}

Output:


A is an alphabetic character.

In this example, the code checks if the character 'A' is alphabetic using the isalpha function, and since it is indeed an alphabetic character, the program prints the message confirming that 'A' is an alphabetic character.

2. Character Conversion Functions (in <ctype.h>):
  • 'tolower': Converts an uppercase character to its lowercase equivalent.
  • 'toupper': Converts a lowercase character to its uppercase equivalent.

Example:


#include <stdio.h>
#include <ctype.h>

int main() {
    char ch = 'a';
    char uppercaseCh = toupper(ch);
    printf("%c converted to uppercase is %c.\n", ch, uppercaseCh);
    return 0;
}

Here's the output of the example code, which uses character conversion functions from <ctype.h> to convert a lowercase character to uppercase:


a converted to uppercase is A.

In this example, the code converts the lowercase character 'a' to its uppercase equivalent using the toupper function and then prints the original character and the converted uppercase character.

3. String Manipulation Functions (in <string.h>):
  • strlen: Calculates the length of a string (number of characters excluding the null terminator).
  • 'strcpy': Copies one string to another.
  • 'strcat': Concatenates (appends) one string to another.
  • 'strcmp': Compares two strings lexicographically.
  • 'strchr': Finds the first occurrence of a character in a string.
  • 'strstr': Finds the first occurrence of a substring in a string.

Example:


#include <stdio.h>
#include <ctype.h>

int main() {
	char str1[] = "Hello";
	char str2[] = "World";
	strcat(str1, " "); // Concatenate a space
	strcat(str1, str2);
	printf("Concatenated string: %s\n", str1);

	char sentence[] = "The quick brown fox jumps over the lazy dog";
	char *word = "fox";
	char *found = strstr(sentence, word);
	if (found != NULL) {
		printf("'%s' found in the sentence.\n", word);
	}
	return 0;
}

Output:


Concatenated string: Hello World
'fox' found in the sentence.

In this example:

  • The code concatenates the strings "Hello" and "World" with a space in between using the strcat function, resulting in "Hello World." It then prints the concatenated string.
  • It also searches for the substring "fox" within the sentence "The quick brown fox jumps over the lazy dog" using the strstr function and finds that "fox" is present, so it prints a message confirming the presence of "fox" in the sentence.

These character-oriented library functions are useful for tasks involving character manipulation, validation, and string processing in C programs. They help you work with individual characters and strings of characters efficiently and effectively.