Winter Bootcamp in ML and IoT in Jaipur
 Course content (For Bootcamp & Winter Training):- Machine Learning (ML) || Internet of Things (IoT) || Register for winter bootcamp
0 like 0 dislike
in Tutorial & Interview questions by (3.4k 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 (3.4k 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;

}

Winter 10 Days boot-camp classes(7 HRS Daily) will start from 5, 20 & 27 December 2019 in:
1) Internet of things(IoT) Using RASPBERRY-PI
2) Machine Learning (ML)

70% OFF| Fee-INR 3,000/-

Limited seats!! Hurry up!!

[[ CALL - 07976731765 ]]

Some Study Resources are compiled from original Stack Overflow Documentation, the content is developed by the different experts at Stack Overflow. Study resources are released under Creative Commons BY-SA. Images may be copyright of their respective owners. This website is for self-learning and not affiliated with Stack Overflow. All trademarks and registered trademarks are the property of their respective company owners. Please send feedback and corrections to chandwaglobal@gmail.com.

Goeduhub Important Lists Our Youtube Channels (For free E-learning)
About Us List of IITs Goeduhub Technologies
Contact Us List of NITs AI and Big Data-HADOOP E-Learning Series
  List of PSUs Smart Learning PLC-SCADA, IoT and Raspberry-PI
  List of Exams After Graduation  
...