- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个指针 char * c =“我 - 需要 - 做 - 这个 - 中断”。我需要根据“-”来打破它,以便在每次迭代中我得到的输出为“I”,然后是“I - need”,然后是“I - need - to”,依此类推,直到整个字符串。有什么帮助吗?
最佳答案
简单的方法是使 c
可变,而不是指向字符串文字的指针。这样,您所需要做的就是沿着字符串进行操作(使用指针或索引)并跟踪您是否在单词中。当您点击空格(或连字符,如果去掉空格),如果您在一个单词中,则保存当前字符,用 '\0'
覆盖数组中的当前字符以终止当前字符处的字符串并打印它。恢复数组中的当前字符并重复直到用完字符,例如
#include <stdio.h>
int main (void) {
char c[] = "I - need - to - do - this - break", /* an array */
*p = c; /* pointer to current char */
int in = 0; /* flag for in/out of word */
while (*p) { /* loop over each char */
if (*p == ' ' || *p == '-') { /* if a space or hyphen */
if (in) { /* if in a word */
char current = *p; /* save current char */
*p = 0; /* nul-terminate array at current */
puts (c); /* print array */
*p = current; /* restore current in array */
in = 0; /* set flag out of word */
}
}
else { /* otherwise, not a space or hyphen */
in = 1; /* set flag in word */
}
p++; /* advance to next char */
}
if (in) /* if in word when last char reached */
puts (c); /* output full string */
}
示例使用/输出
$ ./bin/incremental
I
I - need
I - need - to
I - need - to - do
I - need - to - do - this
I - need - to - do - this - break
使用非可变字符串文字
如果您必须使用不可变的字符串文字,那么方法基本上是相同的。唯一的区别是您不能终止原始字符串,因此您只能使用另一个指针(或索引)从头开始输出每个字符,直到使用 putchar 到达当前字符(或获取数字)来自 p - c
的字符,然后复制到缓冲区以立即终止并输出)。只需循环直到到达当前位置并使用 putchar
进行输出就和其他操作一样简单,例如
#include <stdio.h>
int main (void) {
char *c = "I - need - to - do - this - break", /* string literal */
*p = c; /* pointer to current char */
int in = 0; /* flag for in/out of word */
while (*p) { /* loop over each char */
if (*p == ' ' || *p == '-') { /* if a space or hypen */
if (in) { /* if in a word */
char *wp = c; /* get pointer to start */
while (wp < p) /* loop until you reach current */
putchar (*wp++); /* output each char & increment */
putchar ('\n'); /* tidy up with newline */
in = 0; /* set flag out of word */
}
}
else { /* otherwise, not a space or hyphen */
in = 1; /* set flag in word */
}
p++; /* advance to next char */
}
if (in) /* if in word when last char reached */
puts (c); /* output full string */
}
(输出相同)
仔细检查一下,如果有疑问请告诉我。
关于c - 打破字符串指针直到c中的标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54730800/
这是主函数,其中还调用了 9 个函数。我将向您展示另一个函数,以便您了解我的目标是什么。 int main() { char B[rows][columns]; char answer
每当我使用 preventDefault() 时,我通常将它放在事件处理程序的顶部,如下所示: $('#foo').on('click', function(e){ e.preventDefaul
我想要实现的是用户输入一个值,然后输入一个测量值。然后将其放置为最小、最大或介于两者之间。保留输入了多少个值的计数。以及以米为单位的所有值的总和。 程序最初可以运行,但是当我输入换行符时,程序会重复同
我无法打破 while 循环。 "; $quizslots = mysql_query("SELECT * FROM quiz_slots WHERE `quizid`=$quizsectio
所以我将一个模板化的二叉树字典实现为一个继承自抽象字典类的类,我的添加函数有一个我无法弄清楚的问题。 基本上,我的树的节点具有键和值,以及指向其父节点、左子节点和右子节点的指针。节点的代码是 stru
我的代码应该在内容之间放置一个横幅,但它正在循环播放横幅。我需要它只显示一个横幅。我试过使用 return false;,就像这个例子一样,但它没有用: $(".newsitem_text").con
我有一个 Storyboard指定用于登录我的应用程序。我将其嵌入到 UINavigationController 中。登录到我的应用程序(并过渡到新的 Storyboard)后,我想“脱离”这个导航
我想知道是否可以在不使用 MCU 复位引脚上的外部复位按钮的情况下中断 while 循环并从特定位置重新启动代码。 下面是当“if”语句为真时我想中断的 while 循环,我正在使用 LCD,并想返回
所以我有这个问题,如果数组中的值高于输入值,它应该做一些事情然后停止循环并且不要触及数组中的剩余值。这是到目前为止的代码: const percentages = []; let enteredVal
我想在“div2”中打断长字,div2 和 div3 的宽度都不能大于父宽度(即 150px)。唯一有效的是 word-break: break-all 但这也会打断短词。 #div1{ di
我的数据库中有 3 个表。 PARENT_A 有一个“ID”主键列。 PARENT_B 有一个“ID”主键列。 CHILD 具有“PARENT_A_ID”和“PARENT_B_ID”外键列。它还有一个
在这个非常人为的示例中,我有一个包含 3 个元素的数组,我使用 .each() 对其进行循环。方法。 var vals = $w('foo bar baz'); vals.each( function
非常简单的示例代码(仅用于演示,没有任何用处): repeat { while (1 > 0) { for (i in seq(1, 100)) { break # usual
我有以下 promise : var aggregatePromise = () => { return new Promise((resolve, reject) => { Ei
我想检测表单的“输入”键而不让表单被提交。我如何打破这种关联? document.forms[0].onkeypress = function (event) { e = window.eve
这里是新手。我有一个 Ajax 函数,可以循环 3 个不同的请求。但是,如果第一个请求失败,我希望其他请求终止。我尝试放入“break”语句,但收到“非法的break语句”错误,我猜测是因为它不是直接
我有一个 Vector的 Vector不同长度的 s W .这些最后的向量包含 0 到 150,000 之间的整数,步长为 5,但也可以为空。我正在尝试计算每个向量的经验 cdf。我可以像这样计算这些
我想知道如何正确地打破 JS 中的 promise 链。 在这段代码中,我首先连接到数据库,然后检查集合是否已经有一些数据,如果没有则添加它们。不要关注一些 actionhero.js 代码..这里并
我有一个 Vector的 Vector不同长度的 s W .这些最后的向量包含 0 到 150,000 之间的整数,步长为 5,但也可以为空。我正在尝试计算每个向量的经验 cdf。我可以像这样计算这些
您可以使用 CompletableFuture 链接运行 block ,如下所示: CompletableFuture .supplyAsync(block1) .thenApply(
我是一名优秀的程序员,十分优秀!