我制作了一个代码,我的目标是将输入单词的空格放在句子中。 我需要用空格补充小词
像:
Three witches watched three watches
tch
输出:
Three wi es wa ed three wa es
我制作了这段代码:
#include<stdio.h>
#define S 8
#define B 50
void main() {
char small[S] = {"ol"};
char big[B] = {"my older gradmom see my older sister"};
int i = 0, j = 0;
for (i = 0; i < B; i++)
{
for(j=0;j<S;j++)
{
if(small[j]!=big[i])
{
j=0;
break;
}
if(small[j]=='\0')
{
while (i-(j-1)!=i)
{
i = i - j;
big[i] = '\n';
i++;
}
}
}
}
puts(big);
}
答案 0 :(得分:0)
首先,在您的例子中,您使用换行符&#39; \ n&#39;而不是空间。
考虑这个简单的例子:
#include<stdio.h>
#define S 8
#define B 50
void main() {
char small[S] = {"ol"};
char big[B] = {"my older gradmom see my older sister"};
int i = 0, j = 0;
int cpt = 0;
int smallSize = 0;
// loop to retrieve smallSize
for (i = 0; i < S; i++)
{
if (small[i] != '\0')
smallSize++;
}
// main loop
for (i = 0; i < B; i++)
{
// stop if we hit the end of the string
if (big[i] == '\0')
break;
// increment the cpt and small index while the content of big and small are equal
if (big[i] == small[j])
{
cpt++;
j++;
}
// we didn't found the full small word
else
{
j = 0;
cpt = 0;
}
// test if we found the full word, if yes replace char in big by space
if (cpt == smallSize)
{
for (int k = 0; k < smallSize; k++)
{
big[i-k] = ' ';
}
j = 0;
cpt = 0;
}
}
puts(big);
}
首先需要检索小数组的实际大小 完成后,下一步是查看内部&#34;大&#34;如果里面有小字。如果我们找到它,那么用空格替换所有这些字符。
如果你想用一个空格替换整个小单词,那么你需要适应这个例子!
我希望这有帮助!
答案 1 :(得分:0)
一种可能的方法是使用指向string
的指针,一个用于读取,一个用于写入。这将允许用单个空格替换任意数量的字符(来自small
的字符)。并且你真的不想嵌套循环,但只有一个来处理来自big
的每个字符。
最后但并非最不重要的是,除了独立环境(内核或嵌入式开发)之外,永远不应该使用void main()
。代码可能变成:
#include <stdio.h>
#define S 8
#define B 50
int main() { // void main is deprecated...
char small[S] = {"ol"};
char big[B] = {"my older gradmom see my older sister"};
int i = 0, j = 0;
int k = 0; // pointer to written back big
for (i = 0; i < B; i++)
{
if (big[i] == 0) break; // do not process beyond end of string
if(small[j]!=big[i])
{
for(int l=0; l<j; l++) big[k++] = small[l]; // copy an eventual partial small
big[k++] = big[i]; // copy the incoming character
j=0; // reset pointer to small
continue;
}
else if(small[++j] == 0) // reached end of small
{
big[k++] = ' '; // replace chars from small with a single space
j = 0; // reset pointer to small
}
}
big[k] = '\0';
puts(big);
return 0;
}
甚至更好(不需要固定大小的字符串):
#include <stdio.h>
int main() { // void main is deprecated...
char small[] = {"ol"};
char big[] = {"my older gradmom see my older sister"};
int i = 0, j = 0;
int k = 0; // pointer to written back big
for (i = 0; i < sizeof(big); i++)
{
if(small[j]!=big[i])
...
答案 2 :(得分:0)
在C字符串中以空字符终止&#39; \ 0&#39;。您的代码在开头(B
和S
)定义了一个不确定的随机数,并迭代了那么多字符而不是字符串实际包含的确切字符数。您可以通过在while
循环中测试字符串的内容来使用字符串终止的事实。
i = 0;
while (str[i]) {
...
i = i + 1;
}
如果您更喜欢for
循环,则可以将其写为for
循环。
for (i = 0; str[i]; i++) {
...
}
您的代码不会将剩余字符串的内容移动到左侧。如果用一个字符ol
替换两个字符,则必须将剩余字符向左移动一个字符。否则你会在字符串中有一个洞。
#include <stdio.h>
int main() {
char small[] = "ol";
char big[] = "my older gradmom see my older sister";
int s; // index, which loops through the small string
int b; // index, which loops through the big string
int m; // index, which loops through the characters to be modified
// The following loops through the big string up to the terminating
// null character in the big string.
b = 0;
while (big[b]) {
// The following loops through the small string up to the
// terminating null character, if the character in the small
// string matches the corresponding character in the big string.
s = 0;
while (small[s] && big[b+s] == small[s]) {
// In case of a match, continue with the next character in the
// small string.
s = s + 1;
}
// If we are at the end of the small string, we found in the
// big string.
if (small[s] == '\0') {
// Now we have to modify the big string. The modification
// starts at the current position in the big string.
m = b;
// First we have to put the space at the current position in the
// big string.
big[m] = ' ';
// And next the rest of the big string has to be moved left. The
// rest of the big string starts, where the match has ended.
while (big[b+s]) {
m = m + 1;
big[m] = big[b+s];
s = s + 1;
}
// Finally the big string has to be terminated by a null
// character.
big[m+1] = '\0';
}
// Continue at next character in big string.
b = b + 1;
}
puts(big);
return 0;
}