Nom de la fonction : strchr
Fichier d'en-tête : <string.h>
Prototype de fonction : char *strchr(const char *str, char c);
Fonction : Rechercher la position de la première occurrence du caractère spécifié dans une chaîne
Paramètres : char *str est la chaîne cible à trouver ;
char c est le caractère à trouver ;
Valeur de retour : renvoie la position où le caractère apparaît pour la première fois en cas de succès ; renvoie NULL en cas d'échec ;
Exemple de programme : Rechercher la première occurrence du caractère spécifié c dans la chaîne chaîne
#include<string.h>#include<stdio.h>intmain(void){charstring[15];//Définir un tableau de caractères char*ptr,c='c';strcpy(string,www.dotcpp.com); //Copier la chaîne ptr=strchr( string,c);//Trouver la première position où le caractère apparaît if(ptr){printf(Thecharacter%cisatposition:%dn,c,ptr-string);}else{printf(Thecharacterwasnotfoundn);}return0 ;}
Résultats en cours d'exécution :
La position caractéristique : 7