This involves two functions, both of which are listed below:
char *catw(char *s1, char sep, char *s2)
{
char s[strlen(s1) + strlen(s2) + 1];
for(int i = 0; i < strlen(s1); i++) {
s[i] = s1[i];
}
s[strlen(s1)] = sep;
for(int j = 1; j <= strlen(s2); j++) {
s[j + strlen(s1)] = s2[j];
}
char *rs = s;
return rs;
}
The above function works fine, it takes two strings and concatenates them together using the character in between them.
char *catw_arr(char *ss[], char sep)
{
char *ar = ss[0];
for(int i = 1; i < strlen(ss); i++)
{
ar = catw(ar, sep, ss[i]);
printf("%s\n", ar);
}
return ar;
}
This function, however, is the problem. It is supposed to take an array of strings, with an int that is the length of the array, and a separator, and concat all the strings together. The printf is in there because I wanted to test what was going on.
here's the main function I'm using:
int main()
{
char *abc[3] = {"a", "b", "c"};
printf("%s\n", catw_arr(abc, 3, '/'));
return 0;
}
This is what it churns out:
a/
?ĶS?/
?ĶS?/
I honestly have no idea what the problem is here. I'm assuming it's concatenating the first string with the separator, but then it's running into some crap that it can't process and starts outputting gibberish.