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
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.
Gokulran said:
4 years ago
When there is two or more number of 'i' in the string what happens? Please explain.
Bhanushree .c.L said:
5 years ago
Can anyone tell me why we are getting here the position value of the character?
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.
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)
Madhu said:
8 years ago
Why ptr= strrchr(text,c);? Please explain it.
Pranali said:
8 years ago
@Tiji K Thomas.
Because array index always starts from 0.
Because array index always starts from 0.
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.
Priya said:
9 years ago
Why ptr-text and why not ptr?
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?
We suppose to store the strrstr in *ptr? Can anyone please explain?
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;
}
#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;
}
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?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers