-1
#include <iostream>

using namespace std;

int ft_strlen(char *str, char ascii[])
{
    int i = 0;
    while (!ascii[(short)str[i]] && str[i])
        ++i;
    return (i);
}

int word_count(char *s, char *ascii) {
    int i = 0, cnt = 0;
    while (s[i])
    {
        if (ascii[(short)s[i]])
            while (ascii[(short)s[i]])
                ++i;
        else {
            ++cnt;
            while (!ascii[(short)s[i]] && s[i])
                ++i;
        }
    }
    return (cnt);
}

char **ft_strsplit(char *s, char *ascii) {
    if (!s)
        return NULL;
    int wc = word_count(s, ascii);
    char **ans = (char **)malloc(wc);
    char *result;
    int i = 0, j = 0, k;
    while (wc--) {
        result = (char *) malloc(ft_strlen(s + i, ascii) + 1);
        for (k = 0; s[i] != '\0'; ++i, ++k) {
            if (!ascii[(short)s[i]])
                result[k] = s[i];
            else {
                result[i] = '\0';
                ans[j++] = result;
                //inside the printf
                printf("%d %s\n", j - 1, ans[j - 1]);
                while (ascii[(short)s[i]])
                    ++i;
                break;
            }
        }
        result[i] = 0;
        ans[j] = result;
    }
    for (int i = 0; i < 5; ++i) {
        printf("%d %s\n", i, ans[i]);
    }
    return ans;
}

int main()
{
    char ascii[256] = {0,};
    char d[] = ", ";
    int i = -1;
    while (d[++i])
        ascii[(short)d[i]] = 1;
    char str[90] = "it, is my, day 2,";
    char** ptr = ft_strsplit(str, ascii);
    for (int i = 0; i < 5; ++i) {
        printf("%d %s\n", i, ptr[i]);
    }
    return 0;
}

I splited string and printing them, inside the printf printed well (plz look at the comment) but another printf's first string output weird.. I don't know why this happening. below is this code's output.

0 it
1 is
2 my
3 day
4 2
0 8P
1 is
2 my
3 day
4 2
0 8P
1 is
2 my
3 day
4 2

I expect all printf output like

0 it
1 is
2 my
3 day
4 2

I tried this code at https://cpp.sh/ this site. if this output depends on compiler plz tell me.

Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
neco_cat
  • 1
  • 1
  • C and C++ are two *very* different languages, and a "string split" function would do very different things and use very different types in the two languages. So please don't tag multiple languages, only the one you're actually program in. – Some programmer dude Oct 28 '22 at 07:53
  • With that said, you use one C++-specific header file and one C++-specific statement, which makes the code C++. ***But*** you don't use any other C++-specific code, all the rest could be almost plain C. What are you trying to learn, C or C++? – Some programmer dude Oct 28 '22 at 07:54
  • 1
    And either way, not even whatever resource you use to learn "C in C++" does a very good job at teaching you C either. The `std::malloc` function allocates a specific number of *bytes*, not "objects". The size of e.g. `char *` is different from the size of `char`. You are simply not allocating enough memory, and go out of bounds of the memory you have allocated. That leads to *undefined behavior*. – Some programmer dude Oct 28 '22 at 07:56
  • Why is `ascii` an array of `char` rather than `bool`? – Alan Birtles Oct 28 '22 at 07:57
  • There are also a lot of other problems, and questionable code. Take for example the `ascii` array, what is it really supposed to represent? Because right now all it contains are a "boolean" equivalence, where only the `','` and `' '` characters are "true". And since you use the local encoding of your system, it might not even correspond or be related to anything about ASCII at all. – Some programmer dude Oct 28 '22 at 08:00
  • I just use cpp.sh this site to compile my c code, now I use https://www.onlinegdb.com/online_c_compiler <- this site and change headers e.g. stdio.h, stdlib.h but same happening occurred. – neco_cat Oct 28 '22 at 08:01
  • now I'm learning C, so I don't use boolean type sorry for confusing you guys – neco_cat Oct 28 '22 at 08:03
  • C have a `bool` type, it was introduced with the C99 standard over 20 years ago. And if you're learning C, why are you using C++ headers and statements? And in C most (if not all) of your casting isn't needed. You do yourself a disservice to mix languages like that while learning. Get some decent C beginners books and learn C properly. – Some programmer dude Oct 28 '22 at 08:10
  • Oh and adding to the list of problems, you need to think about the order in which you check for string terminator. Your `strlen` function can count the null-terminator which isn't supposed to be included in the string length. – Some programmer dude Oct 28 '22 at 08:14

1 Answers1

0

It's been a ghastly number of years since I've done C code, but from what I recall, this line:

char **ans = (char **)malloc(wc);

will only give you one byte for each word in the string, when you actually need a pointer's length. I believe what you actually want there is:

char **ans = (char **)calloc(wc, sizeof(char*));

Also, why are you bothering learning C?? Even if you ignore the object-oriented features of it, C++ is a much "Better C" (that's one of it's design goals), and give you things like bool.

James Curran
  • 101,701
  • 37
  • 181
  • 258
  • do you know ecole 42? this curriculum teaching about C language, so I started learning C 1month ago, and so until now I didn't know C has a bool – neco_cat Oct 28 '22 at 08:22
  • `bool` was added to C in its C99 standard (23 years ago). (10+ years after it was in the C++89 standard). but the number of C programmer who learned it post-C99 is tiny compared to the number who learned the earlier C90 standard, it gets forgotten. – James Curran Oct 28 '22 at 08:32