C Online Compiler
Example: Find Character Example in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Find Character Example #include <stdio.h> #include <string.h> // Required for strchr() int main() { // Step 1: Declare a string and the character to find char text[] = "Programming is fun."; char charToFind1 = 'm'; char charToFind2 = 'x'; // Character not in string // Step 2: Find the character using strchr() char *resultPtr1 = strchr(text, charToFind1); char *resultPtr2 = strchr(text, charToFind2); // Step 3: Print results based on whether the character was found if (resultPtr1 != NULL) { printf("Character '%c' found at position: %ld\n", charToFind1, resultPtr1 - text); printf("Substring from 'm': %s\n", resultPtr1); } else { printf("Character '%c' not found.\n", charToFind1); } if (resultPtr2 != NULL) { printf("Character '%c' found at position: %ld\n", charToFind2, resultPtr2 - text); } else { printf("Character '%c' not found.\n", charToFind2); } // Finding null terminator explicitly char *nullPtr = strchr(text, '\0'); if (nullPtr != NULL) { printf("Null terminator found at position: %ld\n", nullPtr - text); } return 0; }
Output
Clear
ADVERTISEMENTS