Free Online Tutorials ==>> Machine Learning || Python Programming || DBMS || OOPs using C++ || DSA || Java Programming || Linux/Unix || C Programming
Go to your University || Python Lab || DSA Lab || AI & Machine Learning Lab || Linux Lab || OOPs Lab || DBMS Lab || JAVA Lab ||| Free Online Tutorials ||| 
companies for Campus Placement || Logical Reasoning || Quantitative Aptitude || General English || Technical-MCQ and Interview Questions || HR Interview Questions
0 like 0 dislike
15 views
in Tutorial & Interview questions by Goeduhub's Expert (7.8k 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 (7.8k 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;

}
  Realize your learning potential with courses starting at ₹ 420
 Placements:   List of companies | Logical Reasoning Questions | Quantitative Aptitude Questions | General English Questions | Technical-MCQ and Interview Questions
 Online Free Training:  MACHINE LEARNINGPython Programming | Database Management System(DBMS) | Object Oriented Programming(OOPs) using C++ | Data Structures and Algorithms(DSA) | Java Programming | Linux/Unix | C Programming
Exams: List of Exams After Graduation | List of Engineering Entrance Examinations (UG/PG) | JEE Main | JEE Advanced | GATE | IES | ISROList of PSUs
 Download Previous Year Papers For:  GATE | IES | RAJASTHAN TECHNICAL UNIVERSITY (RTU-Kota)RPSC Technical Exams | ISRO
 Goeduhub
About Us | Contact Us   Social::   |  | 
...