-3

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...

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 2
    Again, don't post and repost the same question over and over. That will only lead to fewer people wanting to help you! – Some programmer dude May 04 '22 at 10:56
  • You should probably also take some time to sit down and actually *think* about your assignment, and the problems you have. As well as learning how to *debug* your own code (which is an essential thing to know for all programmers). – Some programmer dude May 04 '22 at 10:57
  • @Someprogrammerdude but this is a new functions. I found a solution to my old one. It's exactly this code but in the other direction from left to right. – TheLuckyGuy May 04 '22 at 10:59
  • 2
    Then rename the question, make the title a short summary of the problem you actually ask about. Also please take some time to read [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). And [edit] your question to tell us the expected behavior and the actual behavior (preferably with examples of input and actual versus expected output). And please try to create a [mre] to show us how you call the function. – Some programmer dude May 04 '22 at 11:01
  • Also note that the real [`strrchr`](https://en.cppreference.com/w/c/string/byte/strrchr) function doesn't reverse the string! So searching for the last `'l'` in the string `"Hello"` should return the string `"lo"`. – Some programmer dude May 04 '22 at 11:03
  • 1
    `for (int i = stringcnt-1; arr[i]; i--)`: when does this loop stop? Think again. – Jabberwocky May 04 '22 at 11:06
  • @Jabberwocky the loop stops when the length of the string is 0. – TheLuckyGuy May 04 '22 at 11:14
  • @TheLuckyGuy wrong, the loop stops when `arr[i]` is 0. Ask yourself what happens once `i` is 0. – Jabberwocky May 04 '22 at 11:28
  • 1
    @Someprogrammerdude after looking into it again it works as expected. – TheLuckyGuy May 04 '22 at 11:28
  • @TheLuckyGuy another issue in your code: `arr[i] = tolower(arr[i]);` actually modifies the original string, and I'm pretty sure it shouldn't. For that reason `KULstrrcichr("Hello", 'e')` won't work. – Jabberwocky May 04 '22 at 11:31
  • @Jabberwocky the excercise says that it has to be case-insensitive thats why I'am converting. – TheLuckyGuy May 04 '22 at 11:34
  • @TheLuckyGuy being case insensitive is one thing. Permanently converting the original string to lower case is another thing. – Jabberwocky May 04 '22 at 12:01

1 Answers1

0

The standard C function strrchr is declared the following way

char *strrchr(const char *s, int c);

That is it first parameter has the qualifier const and the second parameter is of the type int. It means that you may not change the source string.

Also this for loop

for (int i = stringcnt-1; arr[i]; i--)

invokes undefined behavior due to the incorrect condition.

The function can be defined the following way as shown in the demonstration program below.

#include <string.h>
#include <stdio.h>
#include <ctype.h>

char * my_strrchr( const char *s, int c )
{
    c = tolower( ( unsigned char )c );

    const char *p = s + strlen( s ) + 1;

    while (p != s && tolower( ( unsigned char )p[-1] ) != c) --p;

    return p == s ? NULL : ( char * )( p - 1 );
}

int main( void )
{
    char s[] = "helLo";

    char *p = my_strrchr( s, 'l' );

    if (p != NULL)
    {
        printf( "position = %td, substring = %s\n", p - s, p );
    }
}

The program output is

position = 3, substring = Lo
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335