- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
基本上,我使用 strtok()
去除多余的后续空格,然后使用 strcat()
将字符串连接回去,这样就只有一个空格根据需要在字符串中。例如:“你好呀。”字符串有 3 个空格。我的程序将成功 strtok()
字符串,然后将其放回一起,可以这么说,只有一个空格。但是,当字符串看起来像这样时:“你好,你怎么样?”我的程序将输出:“你好”,丢弃标记之间的所有内容。
这是我的代码片段:
void stringFunction(struct dh *header){
int i;
char *spaceTk, *spaceString, *holder;
struct dh *temp;
temp = header->next;
while(temp != NULL){
spaceString = malloc(strlen(temp->string) + 1);
strcpy(spaceString, temp->string);
for(i = 0; i < strlen(spaceString) + 1; i++){
if(spaceString[i] == ' '){
count++; //don't worry about this variable
if(spaceString[i] == ' ' && spaceString[i + 1] == ' '){
spaceTk = strtok(spaceString, " ");
while(spaceTk != NULL){
holder = malloc(strlen(spaceTk) + 1);
strcpy(holder, spaceTk);
spaceTk = strtok(NULL, " ");
}
strcat(spaceString, " ");
strcat(spaceString, holder);
strcpy(temp->string, spaceString);
}
}
}
}
}
......
我知道变量“holder”存储 token ,但被最后一个 token 覆盖。我只是不确定如何保存第一个和最后一个标记之间的单词。
谢谢。
最佳答案
您对strtok()
的使用非常奇怪。通常会让it遍历源字符串来查找分隔符,但您似乎是手动执行此操作,然后才调用strtok()
。
此外,如果你有 strdup()
那么它比 strlen()
+ malloc()
+ 方便得多strcpy()
,具有相同的结果(包括在不再需要时释放已分配存储空间的相同义务)。如果您没有 strdup()
,并且需要动态分配字符串副本,那么您应该考虑编写它。
此外,使用 strcat()
在重叠的对象之间进行复制(正如您所做的那样)会产生未定义的行为。不惜一切代价避免这种情况。由于您已经创建了原始字符串的工作副本,因此避免在重叠对象之间进行复制的一种方法是将这些片段连接到原始字符串中,而不是将它们连接到工作空间中,然后将其复制回原始字符串字符串。
无论如何,您都需要在收到 token 时以某种方式对其进行处理。您当前的代码忽略第一个和最后一个之间的所有标记(也一直泄漏内存)。以下是您的代码的一个变体,效果会更好:
struct dh *temp;
temp = header->next;
while(temp != NULL){
char *spaceString = strdup(temp->string);
// ... need a NULL check on spaceString here, in case allocation failed
char *first_token = strtok(spaceString, " ");
char *next_token = strtok(NULL, " ");
if (next_token) { // else the original string is OK as-is
strcpy(temp->string, first_token);
do {
strcat(temp->string, " ");
strcat(temp->string, next_token);
next_token = strtok(NULL, " ");
} while (next_token);
}
// It is obligatory to free the working string now that we're done with it
free(spaceString);
}
但这仍然相当低效(尽管比你的更好),因为 strcat()
调用都必须通过从头开始扫描来找到字符串的结尾,更不用说因为动态内存分配(这也是一个潜在的故障点)和函数调用开销。编写不存在任何这些问题的就地空白压缩代码并不太难。这可能看起来像这样:
char *start_at = strstr(header->next, " ");
if (start_at) { // else nothing to do
char *lag = start_at + 1; // after the first space
char *lead = lag + 1; // after the second space
int space_count = 2;
do {
if (*lead != ' ') {
// not part of a run of spaces
space_count = 0;
} else if (space_count++) {
// the second or subsequent space in a run of spaces
continue;
}
*lag++ = *lead;
} while (*lead++);
}
关于c - 如何在 C 中的 strtok 标记之间存储子字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39597497/
我正在寻找将数组中的单词与另一个数组中的字典中的单词进行比较,以查找找到的最大单词数 我使用了 strtok,因为两者中的单词都是用空格分隔的,但它不起作用。我需要你的帮助,请 void cherc
我在这个程序上花了几个小时,并花了几个小时在网上搜索我的方法的替代方案,但整个晚上都被崩溃和错误所困扰…… 我有几件事想用这段代码实现。首先我会解释我的问题,然后我会发布代码,最后我会解释我对这个程序
我希望使用 strtok() 解析以下字符串。 DINNER;NME=Tom Johnson;ADRES=1234 Street;TIME="12:00","15:00":Formal 有没有一种方法
这个问题在这里已经有了答案: Nested strtok function problem in C [duplicate] (2 个答案) 关闭 6 年前。 我意识到标题令人困惑,想不出更清晰的措
只是为了好玩,我正在编写一个程序,它将接受用户输入的字符串(甚至可能是文本文档)并打乱字符串中的单词。 我正在尝试使用 strtok 函数来分隔字符串中的每个单词。目前我觉得我当前的 strtok 实
如果我欺骗了主题,我真的很抱歉,我在这里搜索它没有结果。 我有代码 void split(char* str, char* splitstr) { char* p; char sp
strtok() 函数使用静态变量。 因此它不是可重入的。并且不能用于解析多个字符串。 我想知道的是为什么 strtok() 是这样设计的? strtok_r() 应该是 strtok()。 这个函数
我是 C 编程的新手,目前正在努力使用 strtok。我想使用以下代码将一个字符串拆分为两个字符串(字符串是例如“Bat1:185”,分隔符是冒号): char batName[13]; char b
我正在写一个 C 程序来研究函数的用法 strtok() .这是我的代码: #include #include main() { char abc[100] = "ls &"; c
我正在尝试分割以下类型的行: GM 1 2 3 ! this is a comment 分离出评论部分。有几种可能的注释分隔符:!、' 和 #。 strtok 是明显的解决方案: card->card
我需要将 C 字符串划分为标记。我认为 strtok 将是我最好的尝试,但我得到了非常奇怪的结果...... 这是我的测试程序。在这个例子中,我将得到 3 个带有“##”分隔符的 token ,但是当
我正在使用库的 strtok(...) 并且它似乎工作正常,直到结束条件,导致段错误和程序崩溃。 API 声称 strtok(...) 将在找不到更多标记时输出 NULL,这意味着,我认为,您必须捕获
我有包含歌曲详细信息的链接列表,想要比较两个歌曲名称...附加任务是通过比不同单词更多的相似单词来比较它们...我使用 strtok 逐字获取它,然后检查如果当前单词包含在另一个名称中...但是当我这
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我尝试编写一个函数,该函数获取指向字符数组的指针,从用户读取字符串并删除字符串开头的所有空格,直到出现第一个不是字符串的字符。最后返回开头不带空格的字符串副本。 例如, 对于输入abcd,函数应返回指
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 3 年前。 Improve th
当我尝试分离 token “(555) 333-444”时,下面的代码没有给出所需的输出。我需要函数来分别分离所有这些 555、333、444。我的错误可能是什么? 输出: 555 333-444 #
这个问题已经有答案了: How does strtok() split the string into tokens in C? (16 个回答) 已关闭 7 年前。 抱歉,这可能是一个愚蠢的问题,但
#include #include #include #define MAX_INPUT_LENGTH 1024 char *readline(FILE* fp, int max_length)
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
我是一名优秀的程序员,十分优秀!