1

I'm new to C and started making a program. Here's how it looks like:

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

int main() {
    char input[10000];
    printf("  >>> ");
    gets(input);
    printf("\n\n");
    if (strlen(input) > 10000) {
        printf(" Input too long (INPUT LENGTH = %d)\n", strlen(input));
        return 0;
    } else {
        char processed_input[10000];
        strcpy(processed_input, strsep(input, '$')); //The error is thrown here every time.
        if (processed_input[0] == *"calculate") {
            printf("helo");
            return 0;
        }
    }

    return 0;
}

It isn't completed yet, but my progress stopped when I tried to test run it and it threw this error:

warning: implicit declaration of function 'strsep' [-Wimplicit-function-declaration]
         strcpy(processed_input, strsep(input, '$'));
                                 ^~~~~~

warning: passing argument 2 of 'strcpy' makes pointer from integer without a cast [-Wint-conversion]

note: expected 'const char *' but argument is of type 'int'
 _CRTIMP __cdecl __MINGW_NOTHROW  char *strcpy (char *, const char *);
                                        ^~~~~~

Can someone please tell me how to fix it?

I tried ChatGPT, other questions of people in Stack Overflow and other platforms, but I still can't find out how to fix it.

1 Answers1

3

strsep() is not a standard C function, it is available on most unix systems, including linux and macOS, but it might not be present on Windows or other legacy systems.

Like other unix functions, it might just have been renamed for historical reasons on Windows, so you can try adding this after the #include lines:

#ifdef _MSC_VER
#define strsep(a, b)  _strsep(a, b)
#endif

If this does not work, use my public domain implementation posted below.

For clarity, here is an abstract of the BSD manual page for strsep:

SYNOPSIS

#include <string.h>

char *strsep(char **stringp, const char *delim);

DESCRIPTION

The strsep() function locates, in the string referenced by *stringp, the first occurrence of any character in the string delim (or the terminating '\0' character) and replaces it with a '\0'. The location of the next character after the delimiter character (or NULL, if the end of the string was reached) is stored in *stringp. The original value of *stringp is returned.

An "empty" field (i.e., a character in the string delim occurs as the first character of *stringp) can be detected by comparing the location referenced by the returned pointer to `\0'.

If *stringp is initially NULL, strsep() returns NULL.

HISTORY The strsep() function is intended as a replacement for the strtok() function. While the strtok() function should be preferred for portability reasons (it conforms to ISO/IEC 9899:1990 ("ISO C90")) it is unable to handle empty fields, i.e., detect fields delimited by two adjacent delimiter characters, or to be used for more than a single string at a time. The strsep() function first appeared in 4.4BSD.

Note also that your program has other problems:

  • you should not use gets()
  • strsep() takes the address of a char * as its first argument and a string of separators as the second.

Here is a modified version:

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

#ifdef _MSC_VER
char *strsep(char **stringp, const char *delim) {
    char *start = *stringp;
    if (start) {
        /* skip all characters in the token (not delimiters) */
        char *p = start + strcspn(start, delim);
        if (*p) {
            *p++ = '\0';
            *stringp = p;
        } else {
            *stringp = NULL;
        }
    }
    return start;
}
#endif

int main(void) {
    char input[10000];

    printf("  >>> ");
    if (!fgets(input, sizeof input, stdin)
        return 1;
    printf("\n\n");

    char *ptr = input;
    char *token;
    while ((token = strsep(&ptr, "$")) != NULL) {
        if (!strcmp(token, "calculate")) {
            printf("hello!\n");
            return 0;
        }
    }
    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • 1
    @chux-ReinstateMonica: I'm afraid you are describing a behavior similar to `strtok` which considers sequences of delimiters to act as a single delimiter. `strsep` does not behave this way: `",,"` is parsed as containing 3 empty fields if `delim` contains a comma. – chqrlie May 31 '23 at 09:18