c check first character of string
Checking the First Character of a String in C
To check the first character of a string in C, you can use array indexing. Here's a simple example:
#include <stdio.h>
int main() {
char str[] = "example";
if (str[0] == 'e') {
printf("The first character is 'e'\n");
} else {
printf("The first character is not 'e'\n");
}
return 0;
}
In this example, we declare a character array str
and initialize it with the string "example". We then use str[0]
to access the first character of the string and compare it with the character 'e'. Depending on the comparison, we print the appropriate message.
[[SOURCE 1]]