Home > Enterprise >  I am trying to implent my own strpbrk function in C
I am trying to implent my own strpbrk function in C

Time:08-26

The function is supposed to work similarly to the strpbrk function in C. When I run the code, I get a segmentation fault. I did a rubber duck debug yet couldn't figure out any problem with the code. I am using a gcc compiler on WSL. The main.h header file contains the function declaration char *_strpbrk(char *s, char *accept). Please what am I missing?

#include "main.h"

/**
 * _strpbrk - searches the string s for any of a set of bytes
 *
 * @s: String to be searched
 * @accept: Substring of bytes to search for
 * Return: Return a pointer to the byte in s that matches one of the bytes in
 * accept, or NULL if no such byte is found
 */

char *_strpbrk(char *s, char *accept)
{
    int i, j, check = 0, position = 0;

    i = 0;
    while (*(s   i) != '\0')
    {
        for (j = 0; *(accept   j) != '\0'; j  ) /* Check if character of s in focus is a character in accept. Break out of the for loop if true*/
        {
            if (*(s   i) == *(accept   j))
            {
                check = 1;
                position = i;
                break;
            }
        }

        if (check == 1) /* check if the character of s in focus was found to be in accept. Break out of the while loop if true */
        {
            break;
        }
        i  ;
    }

    if (position == 0) /* Check for return value. Return null if whole string traversed*/
    {
        return ('\0');
    }
    else
    {
        return (s   position);
    }
}

Forgot to mention that the gcc flags -Wall -pedantic -Werror -Wextra -std=gnu89 are used during compilation.

CodePudding user response:

  1. Do not use _ as a prefix.
  2. This task shows how useful is use of the functions
char *mystrchr(const char *str, const char c)
{
    while(*str)
    {
        if(*str == c) return (char *)str;
        str  ;
    }
    return NULL;
}

char *mystrbrk(const char *str, const char *brk)
{
    while(*str)
    {
        if(mystrchr(brk, *str)) return (char *)str;
        str  ;
    }
    return NULL;
}

Now your code:

return ('\0');

This is not returning pointer. You are lucky as '\0' will convert to pointer NULL. Basically, it is very hard to analyze as you overthink many things.

CodePudding user response:

#include "main.h"

/**
 * _strpbrk - searches the string s for any of a set of bytes
 *
 * @s: String to be searched
 * @accept: Substring of bytes to search for
 * Return: Return a pointer to the byte in s that matches one of the bytes in
 * accept, or NULL if no such byte is found
 */

char *_strpbrk(char *s, char *accept)
{
    int i, j, check = 0, position = 0;

    i = 0;
    while (*(s   i) != '\0')
    {
        for (j = 0; *(accept   j) != '\0'; j  ) /* Check if character of s in focus is a character in accept. Break out of the for loop if true*/
        {
            if (*(s   i) == *(accept   j))
            {
                check = 1;
                position = i;
                break;
            }
        }

        if (check == 1) /* check if the character of s in focus was found to be in accept. Break out of the while loop if true */
        {
            break;
        }
        i  ;
    }

    if (position == 0) /* Check for return value. Return null if whole string traversed*/
    {
        return ('\0');
    }
    else
    {
        return (s   position);
    }
}
  •  Tags:  
  • c
  • Related