gpt4 book ai didi

c - 在处理 strcpy(string, "") 时,字符串未被正确清空和分配

转载 作者:行者123 更新时间:2023-12-02 08:12:14 24 4
gpt4 key购买 nike

编辑:我确实尝试将行 arr_of_strings[arr_index_count] = first_word; 更改为 strcpy(arr_of_strings[arr_index_count], first_word); 但随后它给出了一个分段打印 Word is: This

后出现错误

编辑 2:我尝试在没有 strtok 的情况下执行此操作,因为我认为这是了解 C 字符串的好方法。

尝试自学 C。决定创建一个函数,它接受一个字符串,并将字符串中的每个单词放入数组中的一个元素中。这是我的代码:

假设 #define MAX_LENGTH = 80

// char *string_one[unknown_size];

// first_word will represent each word in the sentence
char first_word[MAX_LENGTH + 1] = "";

// this is the array I will store each word in
char *arr_of_strings[MAX_LENGTH];

int index_count = 0;
int arr_index_count = 0;

char sentence[] = "This is a sentence.";

for (int i = 0; i<MAX_LENGTH; i++) {
printf("Dealing with char: %c\n", sentence[i]);

if (sentence[i] == '\0') {
// end of sentence
break;
} else if (sentence[i] == ' ') {
// this signifies the end of a word
printf("Word is: %s\n", first_word);
arr_of_strings[arr_index_count] = first_word;
// after putting the word in the string, make the word empty again
strcpy(first_word, "");
// verify that it is empty
printf("First word is now: %s\n", first_word);

index_count = 0;
arr_index_count++;
} else {
// not the start of a new string... so keep appending the letter to first_word
printf("Letter to put in first_word is: %c\n", sentence[i]);
first_word[index_count] = sentence[i];
index_count++;
}
}

printf("-----------------\n");
for (int j = 0; j<=arr_index_count; j++) {
printf("%s\n", arr_of_strings[j]);
}

打印出来的是:

Dealing with char: T
Letter to put in first_word is: T
Dealing with char: h
Letter to put in first_word is: h
Dealing with char: i
Letter to put in first_word is: i
Dealing with char: s
Letter to put in first_word is: s
Dealing with char:
Word is: This
First word is now:
Dealing with char: i
Letter to put in first_word is: i
Dealing with char: s
Letter to put in first_word is: s
Dealing with char:
Word is: isis
First word is now:
Dealing with char: a
Letter to put in first_word is: a
Dealing with char:
Word is: asis
First word is now:
Dealing with char: s
Letter to put in first_word is: s
Dealing with char: e
Letter to put in first_word is: e
Dealing with char: n
Letter to put in first_word is: n
Dealing with char: t
Letter to put in first_word is: t
Dealing with char: e
Letter to put in first_word is: e
Dealing with char: n
Letter to put in first_word is: n
Dealing with char: c
Letter to put in first_word is: c
Dealing with char: e
Letter to put in first_word is: e
Dealing with char: .
Letter to put in first_word is: .
Dealing with char:
-----------------
sentence.
sentence.
sentence.

如果我们看这里:

First word is now: 
Dealing with char: i
Letter to put in first_word is: i
Dealing with char: s
Letter to put in first_word is: s
Dealing with char:
Word is: isis
  1. 为什么当 word 为空时,我们将 is 放入其中,word 现在是 isis? (与 asis 相同)。

  2. sentence 这个词怎么打印了 3 次?我的算法显然有缺陷,但如果有的话,单词 sentence 不应该打印 4 次(句子中的每个单词一次:This is a sentence)吗?

另外,我刚学C,所以如果有任何其他改进算法的方法,请告诉我。

最佳答案

arr_of_strings 只是一个 char 指针数组,然后您将所有单词指向数组 first_word。此外,您不使用 C 字符串所需的空终止符。


这是一种可能对您有帮助的方法,它使用 strtok :

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

#define N 100
#define LEN 20 // max length of a word

int fill(char matrix[N][LEN], char* data)
{
// How many words in 'data'?
int counter = 0;
char * pch;
// Splits 'data' to tokens, separated by a whitespace
pch = strtok (data," ");
while (pch != NULL)
{
// Copy a word to the correct row of 'matrix'
strcpy(matrix[counter++], pch);
//printf ("%s\n",pch);
pch = strtok (NULL, " ");
}
return counter;
}

void print(char matrix[N][LEN], int words_no)
{
for(int i = 0; i < words_no; ++i)
printf("%s\n", matrix[i]);
}

int main(void)
{
char data[] = "New to the C programming language";
// We will store each word of 'data' to a matrix, of 'N' rows and 'LEN' columns
char matrix[N][LEN] = {0};
int words_no;
// 'fill()' populates 'matrix' with 'data' and returns the number of words contained in 'data'.
words_no = fill(matrix, data);
print(matrix, words_no);
return 0;
}

输出:

New
to
the
C
programming
language

关于c - 在处理 strcpy(string, "") 时,字符串未被正确清空和分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45683588/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com