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 1 of 2.

Rishab said:   1 decade ago
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;
}

Krishna said:   1 decade ago
"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:   1 decade ago
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.

Surbhi said:   7 years ago
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.
(1)

Sarfaraz said:   1 decade ago
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.

Priya said:   9 years ago
By the way why we are using ptr = strrstr(text, c)?

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

Silambarasi said:   1 decade ago
While printing the value of ptr and text separately with %d, their address is printed then how ptr-text print 19?

Umesh said:   8 years ago
In this program, ptr position for i in the above example would be 20 and text index will be 1. So 20 - 1 = 19.

Deepak said:   1 decade ago
ok ststr(); is for first occurence and strrchr(); is for last...got it thanks for information

Niveditha said:   1 decade ago
strstr() used to find the first occurrence.

strrstr() used to find the last occurrence.


Post your comments here:

Your comments will be displayed after verification.