Which of the given function is used to return a pointer to the located...
The correct answer is option 'A' (strrchr()) and not option 'D' (strchar()).
- The function strrchr() is used to return a pointer to the last occurrence of a specific character in a given string.
- This function takes two arguments: a pointer to the string in which to search for the character, and the character to be located.
- The function starts from the end of the string and searches backwards until it finds the specified character or reaches the beginning of the string.
- If the character is found, the function returns a pointer to that character in the string. If the character is not found, the function returns a null pointer.
Here is an example usage of strrchr():
```c
#include
#include
int main() {
char str[] = "Hello, world!";
char ch = 'o';
char* ptr = strrchr(str, ch);
if (ptr != NULL) {
printf("Character '%c' found at position: %ld\n", ch, ptr - str);
} else {
printf("Character '%c' not found in the string.\n", ch);
}
return 0;
}
```
In the above example, the strrchr() function is used to search for the last occurrence of the character 'o' in the string "Hello, world!". Since the last occurrence of 'o' is at position 8, the function returns a pointer to that position. The program then prints the position as output.
Therefore, the correct answer is option 'A' (strrchr()).
Which of the given function is used to return a pointer to the located...
The strchr() function is used to return a pointer to the located character if character does not occur then a null pointer is returned.