我在网上评估的问题上遇到了很多麻烦:
编写C程序以从输入的字符串中删除单词the
的出现
问题在于问题不再给出规范(区分大小写的句子意味着实际的单词在英语中有意义的顺序等等)
问题如上所述,因此我必须假设“the”是指自己的词,而不是“then”中的另一个子串的一部分。
考虑到所有批评和提示,我将以下代码放在一起
#include<stdio.h>
#include<string.h>
int main()
{
int i, j = 0, k = 0,n = 0;
int flag = 0;
char str[100], new[100], word[20]="the ";
printf("\n\nEnter Any String: ");
scanf("%[^\n]",str);
for(i = 0 ; str[i] != '\0' ; i++)
{
k = i;
while(str[i] == word[j])
{
i++,j++;
if(j == strlen(word))
{
flag = 1;
break;
}
}
j = 0;
if(flag == 0)
i = k;
else
flag = 0;
new[n++] = str[i];
}
new[n] = '\0';
printf("\n\nAfter Removing Word From String: %s\n",new);
return 0;
}
这仍然没有得到完全接受,如果字符串只包含单词“the”,或以“the”结尾,则不起作用(因为我正在搜索“the”)。< / p>
答案 0 :(得分:-1)
我不明白你的任务,但你有两个功能: 一个是删除空格(只是单词之间的一个),另一个是从字符串中删除给定的单词。字符串不能是const或字符串文字。
#include <stdio.h>
#include <string.h>
#include <ctype.h>
char *removespaces(char *str)
{
//only one space between the words is allowed
char *ptr = str;
while(*ptr)
{
while(*ptr == ' ' && *ptr) memmove(ptr, ptr + 1, strlen(ptr + 1) + 1);
while(*ptr != ' ' && *ptr) ptr++;
ptr++;
}
return str;
}
char *removestr(char *str, const char *word)
{
char *ptr = str;
size_t len = strlen(word);
while((ptr = strstr(ptr, word)))
{
if(isalnum(*(ptr + len)) || (str != ptr && isalnum(*(ptr -1))))
{
ptr += len;
}
else
{
memmove(ptr, ptr + len, strlen(ptr + len) + 1);
}
}
return str;
}
int main(void) {
char str[] = "the dgffe the ghgfhfgh the rreret the ret there er the";
printf("%s\n", removespaces(removestr(str,"the")));
return 0;
}
答案 1 :(得分:-1)
我希望实际上有一种简单的方法可以解决这个问题,我知道“删除”总是具有相同的值。
确实有!字符串the
可以使用Deterministic Finite Automaton(DFA)进行识别,因此状态机可以直接的方式解决问题,只需最少的内存使用量并且只需输入一次:
#include <stdio.h>
int main()
{
enum State { S, T, H } state = S;
int input;
for (;;) {
input = getchar();
if (input == EOF)
break;
if (state == S) {
if (input == 't')
state = T;
else
putchar(input);
} else if (state == T) {
if (input == 'h')
state = H;
else if (input == 't')
putchar('t');
else {
state = S;
putchar('t');
putchar(input);
}
} else {
if (input == 'e')
state = S;
else if (input == 't') {
state = T;
putchar('t');
putchar('h');
} else {
state = S;
putchar('t');
putchar('h');
putchar(input);
}
}
}
return 0;
}
根据您想要如何处理周围的空格/标点符号/ ...,您可能需要调整DFA - 但由于您没有指定确切的详细信息,我选择只删除所有出现的{{1对于这个例子。
答案 2 :(得分:-1)
Sentence =“一些奇怪的旧句子,带有单词和空格。也许,单词用标点符号分隔,(有时)用多个空格分隔。那你怎么能成为一个好文字匠?”
Word_to_remove =“words”
下面的算法效率不高,但应该很容易理解。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
void remove_word(char *sentence, const char *word);
int main()
{
char sentence[] = " Some weird old sentence with words and spaces. Perhaps, the wordswords swords, and words are separated with punctuation, and (sometimes, paranthetical words) with more than one space. So how can you be a good wordsmith? with an end of words";
const char word[] = "words";
remove_word(sentence, word);
fprintf(stderr, "'%s'\n", sentence);
return 0;
}
void remove_word(char *sentence, const char *word)
{
char *p;
size_t s_len = strlen(sentence);
size_t w_len = strlen(word);
int haveword = 1; /* we assume first bit is a word, this gets fixed */
if(s_len < w_len )
return;
for(p = sentence; p && *p!='\0'; p++)
{
if(isspace(*p) || ispunct(*p)) {
haveword = 1; /* next bit will be a word! */
continue;
}
if(haveword /* is this a new word? */
&& strncmp(word, p, w_len) == 0 /* is it our word? */
&& (w_len <= s_len - (p - sentence)) && !isalnum(*(p+w_len)) /* for sure ? */
) {
memmove(p, p+w_len, s_len - (p - sentence));
s_len = strlen(sentence);
}
haveword = 0;
}
}