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.
Niveditha said:
1 decade ago
strstr() used to find the first occurrence.
strrstr() used to find the last occurrence.
strrstr() used to find the last occurrence.
Tiji k thomas said:
1 decade ago
Why is the position of 'i' 19 and not 20?
Praveen said:
1 decade ago
Because the index value starts from 0 in c language...
Deepak said:
1 decade ago
ok ststr(); is for first occurence and strrchr(); is for last...got it thanks for information
Prem kr said:
1 decade ago
Please tell me how it comes 19?
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
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.
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:
1 decade ago
What is the meaning of that ptr-text?
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.
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:
1 decade ago
Explain the ptr-text clearly with the above example program please.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers