strchr(): searching for matching character

#include <stdio.h>
#include <string.h>
int main(void){
   char str[100]="Welcome to plus2net";
   char *p; // declaring a pointer variable 
   p = strchr(str,'m');
   printf("value  :%c \n",*p); // output is m  
   printf("Address :%d",p); // output is address of m  
   return 0;
}
In the code above in first printf(), we are printing the value at the address stored in our pointer variable p. In second printf() we are printing the address.
#include <stdio.h>
#include <string.h>
int main(void){
   char str[100]="Welcome to plus2net";
   char *p;
   p = strchr(str,'m');
   printf("Position :%d",p-str+1);
   return 0;
}
The output of above code is here
Position :6
In above code, by subtracting the pointer of main string and by adding 1 to it , we will get the position of matching character related to main string.

We can search and get the pointer of first occurance of mathching character by using strchr() string function.
strchr(main_string, search_string);
Note that strchr() returns the address of first matching char, so we have to use one pointer variable to store the return value of this function.
Returns null if searched character is not found.
As we get a pointer by using strchr() , so we can display the string value so rest of the string starting from searched character will be returned.
#include <stdio.h>
#include <string.h>
int main(void){
   char str[100]="Welcome to plus2net";
   char *p;
   p = strchr(str,'m');
   printf("Position :%d , %s",p-str+1,p);
   return 0;
}
Output is here
Position :6, me to plus2net

Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here




    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer