Welcome to our deep dive into C Character Functions! In this lesson, we'll explore a variety of built-in functions in C that help you work with individual characters within a program. Let's get started! 🎉
Character functions are essential in C programming as they allow us to manipulate individual characters, which is crucial when dealing with strings, files, and user input.
Understanding Character Variables
Key Character Functions
int ctype(int ch)int isalpha(int ch)int isdigit(int ch)int isupper(int ch)int islower(int ch)int tolower(int ch)int toupper(int ch)int isspace(int ch)int isprint(int ch)int iscntrl(int ch)int isxdigit(int ch)int isgraph(int ch)int ispunct(int ch)Working with Strings
int strlen(char *str)char *strchr(char *str, int ch)char *strrchr(char *str, int ch)char *strcpy(char *dest, char *src)char *strncpy(char *dest, char *src, size_t n)char *strcat(char *dest, char *src)char *strncat(char *dest, char *src, size_t n)int strcmp(char *str1, char *str2)int strncmp(char *str1, char *str2, size_t n)int strcoll(char *str1, char *str2)int strxfrm(char *dest, char *src, size_t n)char *strtok(char *str, const char *delim)size_t strspn(char *str1, char *str2)size_t strcspn(char *str1, char *str2)Before we dive into functions, let's understand how characters are represented in C. In C, individual characters are stored as integers in a variable of type char.
char myChar = 'A'; // 'A' is an ASCII value of character ANow that we understand character variables, let's explore various character functions and their uses.
Which function is used to check if a character is an alphabetic character?
Here's a practical exercise to help you get comfortable with character functions.
#include <stdio.h>
#include <ctype.h>
int main() {
char myChar = 'A';
printf("Is myChar an alphabetic character? %s\n", (isalpha(myChar) ? "Yes" : "No"));
printf("Is myChar an uppercase letter? %s\n", (isupper(myChar) ? "Yes" : "No"));
char myString[] = "Hello, World!";
printf("Length of myString: %d\n", strlen(myString));
printf("Last occurrence of 'l' in myString: %s\n", strrchr(myString, 'l'));
return 0;
}Save this code as character_functions.c and compile it with the command gcc character_functions.c -o character_functions. Run the program with the command ./character_functions.
Stay tuned for the next part, where we'll explore working with strings using C character functions! 🌟
Happy coding! 🤖