C Programming - Strings - Discussion

Discussion Forum : Strings - General Questions (Q.No. 4)
4.
The library function used to find the last occurrence of a character in a string is
strnstr()
laststr()
strrchr()
strstr()
Answer: Option
Explanation:

Declaration: char *strrchr(const char *s, int c);

It scans a string s in the reverse direction, looking for a specific character c.

Example:

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

int main(void)
{
   char text[] = "I learn through IndiaBIX.com";
   char *ptr, c = 'i';

   ptr = strrchr(text, c);
   if (ptr)
      printf("The position of '%c' is: %d\n", c, ptr-text);
   else
      printf("The character was not found\n");
   return 0;
}

Output:

The position of 'i' is: 19

Discussion:
20 comments Page 2 of 2.

Gokulran said:   5 years ago
When there is two or more number of 'i' in the string what happens? Please explain.

Bhanushree .c.L said:   6 years ago
Can anyone tell me why we are getting here the position value of the character?

Anu said:   1 decade ago
Explain the ptr-text clearly with the above example program please.

Pranali said:   8 years ago
@Tiji K Thomas.

Because array index always starts from 0.

Praveen said:   1 decade ago
Because the index value starts from 0 in c language...

Madhu said:   8 years ago
Why ptr= strrchr(text,c);? Please explain it.

Tiji k thomas said:   1 decade ago
Why is the position of 'i' 19 and not 20?

Monu utpal gupta said:   1 decade ago
What is the meaning of that ptr-text?

Prem kr said:   1 decade ago
Please tell me how it comes 19?

Priya said:   10 years ago
Why ptr-text and why not ptr?


Post your comments here:

Your comments will be displayed after verification.