Gadgets 4 Students Career Guide Free Tutorials  Go to Your University  Placement Preparation 
0 like 0 dislike
466 views
in Tutorial & Interview questions by Goeduhub's Expert (9.3k points)

The strchr and strrchr functions find a character in a string, that is in a NUL-terminated character array. strchr return a pointer to the first occurrence and strrchr to the last one.

1 Answer

0 like 0 dislike
by Goeduhub's Expert (9.3k points)
 
Best answer
The strchr and strrchr functions find a character in a string, that is in a NUL-terminated character array. strchr return a pointer to the first occurrence and strrchr to the last one.

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

int main(void)

{    

char toSearchFor = 'A';

    /* Exit if no second argument is found. */    

if (argc != 2)    

{        

printf("Argument missing.\n");        

return EXIT_FAILURE;    

}

    {        

char *firstOcc = strchr(argv[1], toSearchFor);        

if (firstOcc != NULL)        

{            

printf("First position of %c in %s is %td.\n",  toSearchFor, argv[1], firstOcc-argv[1]);

                          /* A pointer difference's result is a signed integer and uses the length modifier 't'. */        

}        

else        

{            

printf("%c is not in %s.\n", toSearchFor, argv[1]);        

}    

}

    {        

char *lastOcc = strrchr(argv[1], toSearchFor);        

if (lastOcc != NULL)        

{            

printf("Last position of %c in %s is %td.\n",

toSearchFor, argv[1], lastOcc-argv[1]);        

}    

}

return EXIT_SUCCESS;

}

Outputs (after having generate an executable named pos):

$ ./pos AAAAAAA

First position of A in AAAAAAA is 0.

Last position of A in AAAAAAA is 6.

$ ./pos BAbbbbbAccccAAAAzzz

First position of A in BAbbbbbAccccAAAAzzz is 1.

Last position of A in BAbbbbbAccccAAAAzzz is 15.

$  ./pos qwerty A is not in qwerty.

One common use for strrchr is to extract a file name from a path. For example to extract myfile.txt from C:\Users\eak\myfile.txt:

char *getFileName(const char *path)

{    

char *pend;

if ((pend = strrchr(path, '\')) != NULL)        

return pend + 1;

return NULL;

}

Learn & Improve In-Demand Data Skills Online in this Summer With  These High Quality Courses[Recommended by GOEDUHUB]:-

Best Data Science Online Courses[Lists] on:-

Claim your 10 Days FREE Trial for Pluralsight.

Best Data Science Courses on Datacamp
Best Data Science Courses on Coursera
Best Data Science Courses on Udemy
Best Data Science Courses on Pluralsight
Best Data Science Courses & Microdegrees on Udacity
Best Artificial Intelligence[AI] Courses on Coursera
Best Machine Learning[ML] Courses on Coursera
Best Python Programming Courses on Coursera
Best Artificial Intelligence[AI] Courses on Udemy
Best Python Programming Courses on Udemy

Related questions

 Important Lists:

Important Lists, Exams & Cutoffs Exams after Graduation PSUs

 Goeduhub:

About Us | Contact Us || Terms & Conditions | Privacy Policy || Youtube Channel || Telegram Channel © goeduhub.com Social::   |  | 

 

Free Online Directory
...