if(notfound == 1)
{
int len = strlen(word);
//if(strcmp(word, array)== 0)
if(strcmp(array3,word)==0)
{
word[len - 1] = '\0';
}
if(strcmp(word, array2) ==0)
{
word[len - 1] = '\0';
}
fprintf(NewFile,"%s\n", word);
}
这是我的拼写检查程序的代码,至少是我的大多数问题的部分。通过与Dicitonary进行比较,我的程序可以很好地拼写任何文本文件。此代码中的Word将保留包含文本文件中错误单词的数组。数组3是包含标点符号的单词数组,如下所示:char* array3[] = {"a.", "b.", "c.", "d.", "e.", "f.", "g.", "h."};
我尝试将单词与此数组进行比较以摆脱标点符号(在这种情况下为点,但后来我计划了其余的标点符号照顾)。问题是,如果我的阵列看起来像"。",",","!","?" ,";",strcmp只是跳过它而不是摆脱标点符号。而且我知道我的方法非常简单而且不是很合适,但是当我用#34; c。"进行尝试时,它确实有效。另外,我是c语言的新手
如果一个人能够提供帮助,我真的很感激,因为我现在真的很难解决这个问题几个星期了
答案 0 :(得分:0)
如果word
数组可能只有一个尾随标点字符,则可以使用strcspn
删除该字符。
如果word
数组在数组中有多个标点字符,则可以在循环中使用strpbrk
替换这些字符。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char word[100] = "";
char punctuation[] = ",.!?;";
char *temp = NULL;
strcpy ( word, "text");//no punctuation
printf ( "%s\n", word);
word[strcspn ( word, punctuation)] = '\0';
printf ( "%s\n", word);
strcpy ( word, "comma,");
printf ( "%s\n", word);
word[strcspn ( word, punctuation)] = '\0';
printf ( "%s\n", word);
strcpy ( word, "period.");
printf ( "%s\n", word);
word[strcspn ( word, punctuation)] = '\0';
printf ( "%s\n", word);
strcpy ( word, "exclamation!");
printf ( "%s\n", word);
word[strcspn ( word, punctuation)] = '\0';
printf ( "%s\n", word);
strcpy ( word, "question?");
printf ( "%s\n", word);
word[strcspn ( word, punctuation)] = '\0';
printf ( "%s\n", word);
strcpy ( word, "semicolon;");
printf ( "%s\n", word);
word[strcspn ( word, punctuation)] = '\0';
printf ( "%s\n", word);
temp = word;
strcpy ( word, "comma, period. exclamation! question? semicolon;");
printf ( "%s\n", word);
while ( ( temp = strpbrk ( temp, punctuation))) {//loop while punctuation is found
*temp = ' ';//replace punctuation with space
}
printf ( "%s\n", word);
return(0);
}