gpt4 book ai didi

C - 将字符串(句子)转换为字符串列表

转载 作者:太空宇宙 更新时间:2023-11-04 06:58:14 32 4
gpt4 key购买 nike

我需要将一个句子(例如“Hello world”)复制到一个字符串列表中,意思是复制到一个字符数组中,其中每 2 个单词由 '\0' 分隔。请注意,单词被定义为一行中没有空格的任意数量的字符。

因此,每当我的程序在一行中检测到任意数量的空格(包括仅 1 个空格)时,它应该使用单个 '\0' 代替。

问题是,在我的 target 字符数组中第一次写入 '\0' 后,我无法再写入它了。我猜是因为 '\0' 表示字符串结尾,但在我的例子中,我试图在 char 数组中实现一个字符串列表,所以我必须有 '\0'每2个字之间。

基本上我的问题是如何在放置 '\0' 后继续写入 char 数组?

到目前为止,这是我的代码(如您所见,我还在每次迭代时检查 traget 中是否有足够的空间,但那部分工作正常,所以并​​不是很有趣)

int strListFromWords(const char* source, char* target, int buffSize)
{
if (buffSize < 2) return -1;
char* sCurrentPointer = source;
char* tCurrentPointer = target;
int charsInTarget = 0;
while (*sCurrentPointer != '\0') // While not end of string
{
if (charsInTarget + 2 < buffSize) // if there is enough space in target for current char
{
charsInTarget++;
if (!isspace(*sCurrentPointer)) // if current char isn't space
{
*tCurrentPointer = *sCurrentPointer;
sCurrentPointer++;
tCurrentPointer++;
}
else
{
*tCurrentPointer = '\0'; // PROBLEMATIC LINE put '\0' instead of spcace (in target)

sCurrentPointer++; // goto next char in source
tCurrentPointer++; // goto next position in target
while (isspace(*sCurrentPointer)) // while there are more spaces in a row
{
sCurrentPointer++; // just skip them without messing with target
}
}
}
else
{ // Not enough space
emptyStrList(target);
return 0;
}
}
*tCurrentPointer = '\0';
*(tCurrentPointer + 1) = '\0';
return numStrsInList(target);
}

谢谢,

最佳答案

没有什么可以阻止你写过去的 0。

我使用以下代码段测试了您的函数,它正确返回了 word_count。目标缓冲区将包含以 0 结尾的单词,并在末尾加上一个额外的 0。我想,这就是意图。

#include <conio.h> // for getch()
#include <malloc.h>
#include <string.h>

int main()
{
char* source = " Hello World!\nThis is line number two.\n\n \n \n This is the last line";

size_t buflen = strlen(source);
char* target = (char*)malloc(strlen(source));

int word_count = strListFromWords(source, target, buflen);
printTarget(target);

free(target);
getch();
}

此函数将显示整个目标缓冲区:

void printTarget(const char* target) {
char prev = ' ';
for (int i = 0;; i++) {
if (target[i])
putch(target[i]);
else {
putch('\n');
if (!prev)
break;
}
prev = target[i];
}
}

为了编译,需要做一些小改动:

#include <stdio.h>
#include <ctype.h>

int strListFromWords(const char* source, char* target, int buffSize)
{
if (buffSize < 2) return -1;
char* sCurrentPointer = (char*)source;
char* tCurrentPointer = target;
int charsInTarget = 0;
int numStrsInList = 0;

while (*sCurrentPointer != '\0') // While not end of string
{
if (charsInTarget + 2 < buffSize) // if there is enough space in target for current char
{
charsInTarget++;
if (!isspace(*sCurrentPointer)) // if current char isn't space
{
*tCurrentPointer = *sCurrentPointer;
sCurrentPointer++;
tCurrentPointer++;
}
else
{
*tCurrentPointer = '\0'; // PROBLEMATIC LINE put '\0' instead of spcace (in target)
numStrsInList++;

sCurrentPointer++; // goto next char in source
tCurrentPointer++; // goto next position in target
while (isspace(*sCurrentPointer)) // while there are more spaces in a row
{
sCurrentPointer++; // just skip them without messing with target
}
}
}
else
{ // Not enough space
//emptyStrList(target);
return 0;
}
}

*tCurrentPointer = 0;
*(tCurrentPointer + 1) = 0;
return numStrsInList;
}

请注意,我只回答了被问到的问题。

关于C - 将字符串(句子)转换为字符串列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41631194/

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