热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

c字串:如果在句子中找到一个词。-cstring:put''ifawordfoundinthesentence

Imadeacodeandmytargetistoputspacewheretheinputwordwasfoundinasentence.ineettor

I made a code and my target is to put spacewhere the input word was found in a sentence. i neet to replece the small word with space

我做了一个代码,我的目标是在一个句子中找到输入词的位置。我要用空间来回答这个小问题。

like:

如:

Three witches watched three watches
tch

output:

输出:

Three wi es wa ed three wa es

I made this code:

我做了这个代码:

#include
#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 

3 个解决方案

#1


0  

First of all, in your exemple you work with newline '\n' and not with space.
Consider this simple example:

首先,在你的例子中,你使用的是newline '\n'而不是空间。考虑一下这个简单的例子:

#include
#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 

You need first to retrieve the real size of the small array.
Once done, next step is to look inside "big" if there is the word small inside. If we find it, then replace all those char by spaces.

首先需要检索小数组的实际大小。一旦完成,下一步就是看里面的“大”,如果里面有小单词。如果我们找到它,然后用空格替换所有这些字符。

If you want to replace the whole small word with a single space, then you'll need to adapt this example !

如果您想要用一个空格替换整个小单词,那么您需要调整这个例子!

I hope this help !

我希望这些帮助!

#2


0  

A possible way is to use to pointers to the string, one for reading and one for writing. This will allow to replace an arbitrary number of chars (the ones from small) with a single space. And you do not really want to nest loops but une only one to process every char from big.

一种可能的方法是使用指向字符串的指针,一个用于读取,另一个用于写入。这将允许用一个空格替换任意数量的chars(来自小的)。而且你并不是真的想要嵌套循环,但是只需要一个来处理每个字符。

Last but not least, void main() should never be used except in stand alone environment (kernel or embedded development). Code could become:

最后但并非最不重要的是,除了独立环境(内核或嵌入式开发)之外,不应该使用void main()。代码可能成为:

#include 
#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 

or even better (no need for fixed sizes of strings):

或者更好(不需要固定大小的字符串):

#include 

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 

#3


0  

In C strings are terminated with a null character '\0'. Your code defines a somehow random number at the beginning (B and S) and iterates over that much characters instead of the exact number of characters, the strings actually contain. You can use the fact that the string is terminated by testing the content of the string in a while loop.

在C字符串中,以null字符“\0”结束。您的代码在开头(B和S)定义了一个任意的随机数,并遍历了许多字符,而不是字符的确切数目,字符串实际上包含了。您可以使用一个事实,即字符串通过在while循环中测试字符串的内容而终止。

i = 0;
while (str[i]) {
  ...
  i = i + 1;
}

If you prefer for loops you can write it also as a for loop.

如果你喜欢循环,你也可以把它写成for循环。

for (i = 0; str[i]; i++) {
  ...
}

Your code does not move the contents of the remaining string to the left. If you replace two characters ol with one character , you have to move the remaining characters to the left by one character. Otherwise you would have a hole in the string.

您的代码不会将剩余字符串的内容移动到左侧。如果你用一个字符替换两个字符,你必须将剩下的字符移到一个字符的左边。否则你会在弦上有个洞。

#include 

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;
}

推荐阅读
author-avatar
王责宇0218
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有