C Programming - Library Functions - Discussion

Discussion Forum : Library Functions - General Questions (Q.No. 3)
3.
Which standard library function will you use to find the last occurance of a character in a string in C?
strnchar()
strchar()
strrchar()
strrchr()
Answer: Option
Explanation:

strrchr() returns a pointer to the last occurrence of character in a string.

Example:


#include <stdio.h>
#include <string.h>

int main()
{
    char str[30] = "12345678910111213";
    printf("The last position of '2' is %d.\n",
            strrchr(str, '2') - str);
    return 0;
}

Output: The last position of '2' is 14.

Discussion:
18 comments Page 2 of 2.

Suren said:   1 decade ago
Why we are putting -str there in printf statement? Please help me.

Neha said:   1 decade ago
Thanks shrikant it is really very easy when you explained it.

RENU GURPREET said:   1 decade ago
Thanks @Srikanth.

Sonu said:   1 decade ago
Rightly said srikanth.

Kiran said:   1 decade ago
Excellent srikanth.

Ravi said:   1 decade ago
Thanks srikanth.

Athinivas said:   1 decade ago
@Pruthvi strrchr() returns pointer to last(first in terms of position)occurence of char u given....so if we subtract the add of that returning pointer with the actual one...we get position...(Since char occupies a single byte)....Thanq u..

Pruthvi said:   1 decade ago
Why -str is used?


Post your comments here:

Your comments will be displayed after verification.