1
この関数は、一次元配列を二次元配列に変換します。私はそれを変換するとき、私はすべての4x4の正方形の間に\n
(改行)を保つ必要があります。 str split関数が機能しません
これは、この一次元の文字列に変換されます(const char *s
):
int count_words(const char *str, char c)
{
int i;
int count;
i = 0;
count = 0;
while (str[i])
{
while (str[i] && str[i] == c)
i++;
if (str[i])
{
count++;
while (str[i] && str[i] != c)
i++;
}
}
return (count);
}
char *get_word(const char *str, char c)
{
char *word;
int i;
i = 0;
word = (char*)malloc(sizeof(char) * 500);
while (str[i] && str[i] != c)
{
word[i] = str[i];
i++;
}
word[i] = '\0';
return (word);
}
char **ft_strsplit(const char *s, char c)
{
char **split;
int words;
int i;
i = 0;
words = count_words(s, c);
split = (char**)malloc(sizeof(char*) * words + 1);
if (split == NULL)
return (NULL);
while (*s)
{
if (*s == c)
s++;
if (*s)
{
if (*s == c)
{
split[i] = ft_strdup("\0");
s++;
//printf("=========>%s\n", split[i]);
i++;
}
else{
split[i] = get_word(s, c);
s = s + ft_strlen(split[i]);
//printf("%s\n", split[i]);
i++;}
}
}
split[i] = NULL;
int k = 0;
while (split[k])
{
printf("%s\n", split[k]);
k++;
}
return (split);
}
この
は、入力ファイルである#...\n#...\n#...\n#...\n\n.#..\n.#..\n.#..\n.#..\n\n###.\n..#.\n....\n....\n\n....\n....\n....\n####\n
そして、これはランダムunpritable文字で、出力に含まですそれ。
#...
#...
#...
#...
.#..
.#..
.#..
.#..
�[email protected]��
###.
..#.
....
....
....
....
....
####
なぜこのようなランダムな文字が表示されますか?
SOはデバッグサービスではありません。シンボルでコンパイルするには、デバッガ内でコードを実行し、プログラムを1行ずつトレースして、関連する変数の値を調べ、実際に何が起こっているのかを調べます。 *具体的な質問が発生した場合は、ここに戻って自由に感じてください。 – alk
ああ、これはデバッグサービスです。デバッグは論理エラーを検出しています。これは、OPが繰り返し可能な例とプログラムの完全な説明を提供しているときに私たちが行うことです。 – nicomp
'ft_strsplit'はどうやって呼びますか?改行文字は '\ n'でなければなりません。 –