C Programming - Strings - Discussion

4. 

The library function used to find the last occurrence of a character in a string is

[A]. strnstr()
[B]. laststr()
[C]. strrchr()
[D]. strstr()

Answer: Option C

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


Niveditha said: (Oct 3, 2011)  
strstr() used to find the first occurrence.

strrstr() used to find the last occurrence.

Tiji K Thomas said: (Oct 9, 2011)  
Why is the position of 'i' 19 and not 20?

Praveen said: (Dec 25, 2011)  
Because the index value starts from 0 in c language...

Deepak said: (Jan 13, 2012)  
ok ststr(); is for first occurence and strrchr(); is for last...got it thanks for information

Prem Kr said: (Sep 6, 2012)  
Please tell me how it comes 19?

Krishna said: (Sep 25, 2012)  
"I learn through IndiaBIX.com";

String length=28 (calculate it with space and ."special symbol")

We know that array index start from "ZERO" then (0......27)

char *ptr, c = 'i';
ptr = strrchr(text, c);

Here char c='i';
And function strrchr() return the value of last character occuerence in string .

Now the function match 'i' in the string and return the index value of 'i'. i.e. 19

Madhavikalyanapu said: (Oct 16, 2012)  
printf("The position of '%c' is: %d\n", c, ptr-text);

Here we have to print c that was integer type, but I didn't understand the ptr-text.What is the meaning of that ptr-text?

printf("%d",c);

If we used this statement in the place of 1st printf statement, than also we get c value.

Monu Utpal Gupta said: (Jun 12, 2014)  
What is the meaning of that ptr-text?

Sarfaraz said: (Jun 18, 2014)  
Meaning of ptr-text.

As you can write arr[5] as *(arr+5).

Suppose text pointer was at location 50 in memory and ptr was pointing at 55th location then ptr-text will give 5.

Anu said: (Jul 5, 2014)  
Explain the ptr-text clearly with the above example program please.

Silambarasi said: (Aug 6, 2014)  
While printing the value of ptr and text separately with %d, their address is printed then how ptr-text print 19?

Rishab said: (Nov 26, 2014)  
Code to find last occurrence of i (case insensitive).

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

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

{
if(text[i]>=65 || text[i]<=90)
text[i]+=32;
}

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

Priya said: (Jul 11, 2015)  
By the way why we are using ptr = strrstr(text, c)?

We suppose to store the strrstr in *ptr? Can anyone please explain?

Priya said: (Sep 17, 2015)  
Why ptr-text and why not ptr?

Umesh said: (Nov 9, 2016)  
In this program, ptr position for i in the above example would be 20 and text index will be 1. So 20 - 1 = 19.

Pranali said: (Apr 1, 2017)  
@Tiji K Thomas.

Because array index always starts from 0.

Madhu said: (Jun 5, 2017)  
Why ptr= strrchr(text,c);? Please explain it.

Surbhi said: (Jan 28, 2018)  
ptr = strchr(text, c).

Here ptr will return the memory address of the character but we need to find the position of character.

So we have subtracted array i.e text base address to get the position of character with respect to string.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.