- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
在每第 N 个字符串之间添加字符串。
我的想法:
char *a = "one", *b = "two";
char *word = "Simple sentence containing some random words";
char result[200];
int len = strlen(word);
int spaces = 0;
for(int i = 0; i < len; i++)
{
if(word[i] == ' ')
spaces++;
result[i] = word[i];
if(spaces % 3 == 0)
{
//copy a to result
}
if(spaces % 4 == 0)
{
//copy b to result
}
}
所以在我们准备好之后,结果会是这样的:
Simple sentence containing one some two random words
我已经尝试过 strcpy、strcat 并且我已经为此苦苦挣扎了几天,但我似乎真的不明白这里背后的逻辑。如何进行?
最佳答案
好的,这就是我想出的。代码注释中提供了我所做的一切的详细描述。
代码:
#include <stdio.h>
#include <string.h>
#define MAX_OUTPUT_LENGTH 200
int main(int argc, char const *argv[]) {
/* You can replace the words defined below ("one" and "two") with any words
* you want to insert at the given positions.
*/
char one[] = "one";
char two[] = "two";
char sentence[] = "Longer but still simple sentence containing even more words";
char result[MAX_OUTPUT_LENGTH];
int len = strlen(sentence);
int spaces = 0;
int k = 0; //See Weather Vane's comment for why you also need the variable k
int skipUntilAnotherSpace = 0;
for(int i = 0; i < len; i++)
{
if(sentence[i] == ' ') {
spaces++;
skipUntilAnotherSpace = 0; //See my explanation below for why this is done.
}
if (k == 0) {
result[i] = sentence[i]; //Normally, we can use the variable "i" until our insertion
} else {
/* As Weather Vane's comment shows, after one insertion the positions you
* need to grab from and insert to will change. You cannot continue
* to use "i". After the first insertion:
* Sentence:
* Simple sentence containing some random words
* ^ position 27
* Current value in result variable:
* Simple sentence containing one [you want to insert here]
* ^ position 31
* So, we will need to insert into result at position k, and grab the info
* from a different variable "i".
*/
result[k] = sentence[i];
//Increment k since it will not be incremented regularly in the for loop
k++;
}
if((spaces % 3 == 0) && spaces != 0 && skipUntilAnotherSpace == 0)
{
int useVariableK = 0;
if (spaces > 3) {
/* Since spaces must already have been == 4, we have switched over
to using variable K, so we must continue to do so */
useVariableK = 1;
}
if(!useVariableK) {
result[i] = ' '; //Add a space before we add the word "one"
i++; //Increment i since we added the spaces
int j;
for (j = 0; j < strlen(one); j++) { //Add the word "one"
result[i + j] = one[j];
}
//Increment the variable i the correct # of times since we added the word "one":
i += (strlen (one));
//Add a space after the word "one":
result[i] = ' ';
k = i + 1; //Set k = to i + 1 to account for the space we just added
/* We need to revert back to where the variable "i" was before adding "one":
We used the variable i to temporarily store the positions
as we traversed across and added the word "one". Then, we
moved i back to the original position so we could access
the correct position when using sentence[i] in the next iteration.
Note that we need the +1 in the line below because we actually
need to go back one more position than nessesary; when we re-enter
the loop it will increment i to be +1 again! (sneaky)
*/
i -= (strlen (one) + 1);
/* Why we need the skipUntilAnotherSpace variable:
We cannot simply increment the "spaces" variable after this; we need
the number of spaces to conform to the number in the sentence, and
just because we have more spaces in the result, it woudn't make sense
to increment it for the sentence.
However, if we leave it the same, then the next time we will enter
this loop again since spaces == 3, and we don't want to enter this loop again;
we have done our job already!
So, we make sure not to enter the loop by setting the below variable
to 1. We do not enter the loop unless skipUntilAnotherSpace == 1.
(If we even tried to increment the spaces variable, we would actually
end up going into the next loop because spaces would = 4 ;) )
Finally, we switch back skipUntilAnotherSpace to be 0 once
another space is detected in the sentence.
*/
skipUntilAnotherSpace = 1;
} else {
//Use variable K like we do if spaces == 4:
/* Most of this loop is exactly identical to the previous one, except
that we don't need another space since it was already added before when
"one" was inserted, and we use the variable "k" instead of i. */
int j;
for (j = 0; j < strlen(one); j++) {
result[k + j] = one[j];
}
k += (strlen (one));
result[k] = ' ';
k += 1;
//spaces++;
skipUntilAnotherSpace = 1;
}
}
if((spaces % 4 == 0) && spaces != 0 && skipUntilAnotherSpace == 0)
{
/* Most of this loop is exactly identical to the previous one, except
that we don't need another space since it was already added before when
"one" was inserted, and we use the variable "k" instead of i. */
int j;
for (j = 0; j < strlen(two); j++) {
result[k + j] = two[j];
}
k += (strlen (two));
result[k] = ' ';
k += 1;
//spaces++;
skipUntilAnotherSpace = 1;
}
}
printf("%s.\n", result);
return 0;
}
注意:参见Weather Vane's评论这个问题,进一步理解为什么变量k
是必要的(我在评论中也有解释,我只是觉得Weather Vane的版本更简洁一些)。
这段代码产生的输出是:
Longer but still one simple two sentence containing one even more two words.
如果您将变量 one
和 two
更改为值 "hello" 和 "goodbye"分别地,代码仍然可以运行并产生:
Longer but still hello simple goodbye sentence containing hello even more goodbye words.
基本上,代码在变量 one[]
中每三个单词插入一个值,在变量 two[]
中每四个单词插入一个值。
希望对您有所帮助!
关于c - 每第 N 个单词插入字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38338361/
我有以下数据框 (df_hvl),列名“FzListe”和以下数据: FzListe 7MA1, 7OS1 7MA1, 7ZJB 7MA2, 7MA3, 7OS1 76G1, 7MA1, 7OS1 7
我有点小问题。仅当尝试写入的相同字符串/单词不存在时,我才想写入文件。在我的例子中,它是一个 IP 地址和端口,用“:”分隔。如果我手动写入文件,例如 193...:80 和 193...:22,它指
如何返回结果列中的单词示例? 我得到的最接近的是 [\W]{2,}[^,\W].+[?=,] ID 文本 我的结果(完全匹配) 预期(完全匹配) 1 词A,世界B,词C , 世界 B, 字B 2 wo
我想在引号之间得到一个字符串 我知道一个解决方案是: /'.*?'/ 但问题是它不适用于英语中的所有格或收缩格 例如: What is the name of Mario's brother in t
我应该在句子中找到出现最多的单词。 这是我尝试过的,但不起作用。 '); $max = -1; $resultWords = array(); $resultCount = array(); $i =
我是vim的新手。我正在尝试练习(最近阅读了一些教程),但是我发现我不能不突出显示“复制粘贴”中的字符/单词/行。 在Textmate中,我通常使用SHIFT + CTRL + LeftArrowKe
有谁知道一个JSON格式的英语词典,该词典具有(单词,定义和单词类型,例如名词/形容词/动词/副词) 这种格式: [ {"Word" : "Chair", "Definition" : "A
我正在做一些 javascript,同时我注意到我无法替换 html 标记内的“ document.getElementById('label').innerHTML = document.get
您好,我正在使用 groovy 2.1.5,我必须编写一个代码来显示具有给定路径的目录的内容/文件,然后它会备份文件并替换文件中的单词/字符串。 这是我用来尝试替换所选文件中的单词的代码 String
我正在准备一个实验,我想使用python编写程序以识别参与者说出的某些单词。 我在python中搜索了很多有关语音识别的内容,但结果却很复杂(例如CMUSphinx)。 我要实现的是一个程序,该程序接
假设我有以下代码: $size = 23.9 $size = "$size GB" write $size 我想在其他事情上使用相同的变量,即 if ($size -lt 20) {...} 这显然是
我想替换字符串中单词 Date 的所有情况,除非它是 Date()(即 Date 后跟括号)。这是一个字符串示例以及我最初尝试的内容: x gsub("Date", paste("Date:", S
我对 Java 和编程都很陌生,请记住这一点,请不要对我严厉 ^^。接下来,我最近用 Java 进行了一些培训,我喜欢这个挑战,但现在我只是陷入困境。我做了一些示例来查找用户输入的最大字符串,一切都很
我必须给一个数字x,并写x个字符串(单词)。我必须找到写得最多的那一篇。它可以工作,但是当我尝试从文件中读取它时,却没有。例如,如果我执行 a.out'' #include #include in
这里是学习者,如果这个问题看起来很荒谬,请多多包涵。假设我试图引用字符串中的字符而不是字符串本身,我该怎么做呢?我的意思是; 给定:var str = "我想知道一个大脑分散的计算机如何保持理智" 我
这是阿克沙塔。我一直在解析以下数据。我想单独获取每个单词。我可以有一个示例代码以便我可以继续吗 RTRV-HDR RH01 SIMULATOR 09-11-18 16 13 19 M R
我有一个任意字符串,它总是包含至少一个英文单词后跟一系列数字:"Hello World 1234" 我如何只提取 "Hello World" 来自字符串? 最佳答案 在我看来你需要反正则表达式: St
我正在尝试输入一个四个单词的句子,然后能够使用indexOf和子字符串单独打印出每个单词。有什么想法我做错了吗? 已编辑 那么这就是它应该的样子吗?我已经运行了两次,得到了两个不同的答案,所以我不确定
如何在文本开头查找短语(单词) 我需要非常快速的解决方案来查明文本是否以某些已知短语开头 我在 Mysql (innodb) 表中的短语如下: CREATE TABLE IF NOT EXISTS `
我在 MYSQL 表中有一本字典,该表由 240 000 个单词组成。例如,如果我有字母 G、I、G、S、N> 和 O 我想选择表中包含所有或部分这些字母(并且没有其他字母)的所有单词。 可接受的词语
我是一名优秀的程序员,十分优秀!