从c中的字符串中删除最常见的单词
我需要从C中的字符串中删除所有最常见单词的出现。
如果文本中有几个单词重复相同的次数字符串。省略单词时,您不应省略周围空间和其他字符。如果接收的字符串不包含任何单词,则该函数无需执行任何操作。
一个单词定义为大写和小写字母的数组。 该函数不需要区分大写和小写字母,
的算法是:
- 次
- 的
- 查找最常见的单词出现在字符串中的最常见单词
- 我 最常见的单词
代码:
#include <stdio.h>
#include <limits.h>
#include <ctype.h>
int number_of_word_occurrence(char *s, char *start, char *end) {
int number = 0;
while (*s != '\0') {
char *p = start;
char *q = s;
while (p != end) {
if (*p != *q)break;
p++;
q++;
}
if (p == end)number++;
s++;
}
return number;
}
int length(char *s) {
char *p = s; int number = 0;
while (*p != '\0') {
p++;
number++;
}
return number;
}
char *remove_most_common(char *s) {
int n, max = INT_MIN;
char *p = s;
// Find max occurrence
while (*p != '\0') {
char *start = p;
int word_found = 0;
while (toupper(*p) >= 'A' && toupper(*p) <= 'Z' && *p != '\0') {
word_found = 1;
p++;
}
if (word_found) {
n = number_of_word_occurrence(s, start, p);
if (n > max)max = n;
}
p++;
}
p = s;
int len = length(s);
char *end = s + len;
int i;
// Removing most common word
while (p != end) {
char *start = p, *pp = p;
int word_found = 0;
while (toupper(*pp) >= 'A' && toupper(*pp) <= 'Z' && pp != end) {
word_found = 1;
pp++;
}
if (word_found) {
n = number_of_word_occurrence(s, start, pp);
// If word has max, then remove it
if (n == max) {
while (pp != end) {
*start = *pp;
start++;
pp++;
}
end -= max; // resize end of string
len-=max;
}
}
p++;
}
s[len+=2]='\0';
return s;
}
int main() {
char s[1000] = "Koristio sam auto-stop da dodjem do znaka stop ali prije stopa sam otvorio dekstop kompjutera stop";
printf("%s ", remove_most_common(s) );
return 0;
}
- 应该删除的单词在 BOLD 中
示例1:“ koristio sam auto- 停止 da dodjem do znaka 停止 ali prije stopa sam otvorio dekstop kompjutera kompjutera stop stop stop ”
输出:“ koristio sam auto-da dodjem do znaka ali prije stopa sam otvorio dekstop kompjutera”
示例2:“ 这个是字符串。”
输出:“是字符串。”
示例3:“ 1 pst 1 pst 2 3 pst pstpst pst ”;
输出:“” 11 2 3 PSTPST“
示例4:“ oneeebigggggggwooorrrddd ”;
输出:“”
您能帮我修复我的代码吗?我在删除字符时有一些错误。另外,您还可以帮助我 删除最接近开始的单词?
- 如果所有单词出现都是相同的,则 代码> sprintf sscanf 在求解任务时不允许使用stdio.h库。
I need to remove all occurrences of most common word from string in C.
If there are several words in the text that are repeated the same number of times, the function should remove the one of the most common words that is closest to the beginning of the string. When omitting words, you should not omit surrounding spaces and other characters. If the received string does not contain any words, the function does not need to do anything.
A word is defined as an array of uppercase and lowercase letters.
The function does not need to distinguish between uppercase and lowercase letters
My algorithm is the following:
- find how many times the most common word appears in string
- then go word by word through string
- check if word occurrence is equal to occurrence of most common word
- remove found most common word
Code:
#include <stdio.h>
#include <limits.h>
#include <ctype.h>
int number_of_word_occurrence(char *s, char *start, char *end) {
int number = 0;
while (*s != '\0') {
char *p = start;
char *q = s;
while (p != end) {
if (*p != *q)break;
p++;
q++;
}
if (p == end)number++;
s++;
}
return number;
}
int length(char *s) {
char *p = s; int number = 0;
while (*p != '\0') {
p++;
number++;
}
return number;
}
char *remove_most_common(char *s) {
int n, max = INT_MIN;
char *p = s;
// Find max occurrence
while (*p != '\0') {
char *start = p;
int word_found = 0;
while (toupper(*p) >= 'A' && toupper(*p) <= 'Z' && *p != '\0') {
word_found = 1;
p++;
}
if (word_found) {
n = number_of_word_occurrence(s, start, p);
if (n > max)max = n;
}
p++;
}
p = s;
int len = length(s);
char *end = s + len;
int i;
// Removing most common word
while (p != end) {
char *start = p, *pp = p;
int word_found = 0;
while (toupper(*pp) >= 'A' && toupper(*pp) <= 'Z' && pp != end) {
word_found = 1;
pp++;
}
if (word_found) {
n = number_of_word_occurrence(s, start, pp);
// If word has max, then remove it
if (n == max) {
while (pp != end) {
*start = *pp;
start++;
pp++;
}
end -= max; // resize end of string
len-=max;
}
}
p++;
}
s[len+=2]='\0';
return s;
}
int main() {
char s[1000] = "Koristio sam auto-stop da dodjem do znaka stop ali prije stopa sam otvorio dekstop kompjutera stop";
printf("%s ", remove_most_common(s) );
return 0;
}
- words that should be removed are in bold
EXAMPLE 1: "Koristio sam auto-stop da dodjem do znaka stop ali prije stopa sam otvorio dekstop kompjutera stop"
OUTPUT: "Koristio sam auto- da dodjem do znaka ali prije stopa sam otvorio dekstop kompjutera "
EXAMPLE 2: " This is string. "
OUTPUT: " is string. "
EXAMPLE 3: "1PsT1 psT2 3Pst pstpst pst";
OUTPUT: " "11 2 3 pstpst "
EXAMPLE 4: "oneeebigggwwooorrrddd";
OUTPUT: ""
Could you help me to fix my code? I have some errors while removing characters. Also, could you help me to remove the word closest to the beginning if all of word occurrences are the same?
- Note: Functions from the
string.h
,stdlib.h
libraries, as well as thesprintf
andsscanf
functions from the stdio.h library are not allowed when solving the task. It is not allowed to create auxiliary strings in function or globally.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
所有主要问题都是由于该字符串是信息的源,同时被主动更改。
通常,单词不正确地标记。
用输入
“ Hello World”
作为示例,Hello
,Ello
,llo
,lo
和o
在寻找 word 时将字符串化时,被视为单词。该程序仅在扫描单词时通过一个字符推进字符串。该程序应延长字符串的当前令牌的长度。
number_of_word_occurrence
在进行比较时考虑任何有效 Word 的子字符串。对于输入
the maximum count is incorrectly found to be
5
, forstop
.该问题与上面的问题相加,并开始删除报告这种情况数的错误令牌数据。要概括一下,这种方法的一个很大的问题是,当您从字符串中删除a word 时,下次的出现数量将有所不同找到它。 Looking at
The max occurrence count of a word here is
3
, forhello
.循环删除最大 word 将首次查看Hello
,检查其出现数量,找到它为3
,max和max,并且删除它。对于第二个
Hello
,由于字符串已更改,因此检查其出现计数现在将返回2
。这不是3
的最大值,因此字符串没有变化。此外,由于更改的字符串不会被终止 - 仅此后。意味着搜索一个单词可能会读取过时的数据,从而产生不良结果。
对程序可以使用的功能的严格限制(对动态内存和辅助缓冲区的限制的特殊性)确实可以实现非常详尽的解决方案。
一种解决方案是首先发现字符串中任何 word 的最大出现 count 首先在字符串中出现。然后,做
count
times操作删除 word 的出现,它允许您始终拥有 first 外观作为比较点。通过在字符串中覆盖所有内容(包括null末端字节),将其覆盖 word 。
这是一个粗略的示例 - 在很大程度上未经测试,但为所示示例提供了正确的结果。用
-ddebug
编译以查看其他信息。All the major issues are due to the fact that the string is a source of information, while being actively altered.
In general, words are not tokenized properly.
With the input
"hello world"
as an example, each ofhello
,ello
,llo
,lo
, ando
are considered words when tokenizing the string while looking for the word to remove. The program only advances the string by one character when scanning for words.The program should advance the string by the length of the current token.
number_of_word_occurrence
considers any substring a valid word when making comparisons.For the input
the maximum count is incorrectly found to be
5
, forstop
. This problem compounds with the problem above, and starts removing incorrectly tokenized data that reports this occurrence count.To generalize, a large problem with this approach is that as you remove a word from the string, the number of occurrences is going to be different for that word, the next time it is found. Looking at
The max occurrence count of a word here is
3
, forhello
. Looping through to remove the maximum word will seehello
the first time, check its occurrence count, find it to be3
, the max, and remove it.For the second
hello
, checking its occurrence count now will return2
, since the string was altered. This is not the max of3
, so the string is unchanged.Additionally, the string is not null-terminated as it is altered - only afterwards. Meaning searching for a word might read stale data, giving bad results.
The strict limitation on what functionality the program can utilize (particularity the restrictions on dynamic memory and auxiliary buffers) does make for very exhaustive solutions.
One solution is to first discover the maximum occurrence
count
of any word in the string, and hold a pointer to this word's first appearance in the string. Then, docount
times operations removing the last appearance of the word, which allows you to always have the first appearance as a point of comparison.A word is removed by overwriting it with everything that follows it in the string (including the null-terminating byte).
Here is a cursory example - largely untested, but provides the correct results for the examples shown. Compile with
-DDEBUG
to see additional information.我决定编写一个新功能,该功能从字符串中删除单词的所有出现,这确实表现出您想要的方式。您只需要提供需要删除的源和单词即可。
代码:
输出:
基于此,您应该能够编写零件以找到最常见的单词,存储并将其馈送到此功能。
I decided to write a new function that removes all occurrences of a word from a string, which exactly behaves how you want it to. You only need to provide the source and the word that needs to be removed.
Code:
Output:
Based on this you should be able to write the part to find the most common word, store it, and feed it to this function.