my exercise is to create my own strrchr() function in c.
My solution is a loop. I'am counting the array length. I will input this number into the for loop. For example: With the input Hello. I will go from right to left to search for a letter.
What's not working is the return part.
My code returns the following with the input Hello, and search letter l. lo. That's not what I need. My ouput should be lleH.
Any ideas what I did wrong?
char *KULstrrcichr(char *arr, char search)
// The strrchr() function returns a pointer to the last occurrence of the character c in the string s.
{
int stringcnt;
stringcnt = strlen(arr);
printf("%d\n", stringcnt);
search = tolower(search);
for (int i = stringcnt-1; arr[i]; i--)
{
arr[i] = tolower(arr[i]);
}
for (int i = stringcnt-1; arr[i]; i--)
{
if (search == arr[i])
{
return &arr[i];
}
}
return 0;
}
Okay I found out that my code works as expected...