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);
}
this is my code for the spellchecking program, at least the part which is bring most of my problems. My program is nicely spellchecking any text file, by comparing it with Dicitonary. Word in this code stays for array containing wrong words from a text file. Array 3 is array of words which include punctuation and looks like this: char* array3[] = {"a.", "b.", "c.", "d.", "e.", "f.", "g.", "h."};
I try to compare words with this array to get rid off of the punctuation (in this case dots, but later I planned rest of the punctuation to take care of). The problem is, that if my array would look like ".", ",", "!", "?", ";", the strcmp is just skipping it and not getting rid off of the punctuation. And I know my method is very simple and not really proper, but when I was trying it with "c.", it was working. Plus I am very new to c language
If ayone is able to help, I would really appreciate that, coz I am really stuck with this problem for a weeks now
If the word
array might have a single trailing punctuation character, that character can be removed using strcspn
.
If the word
array has several punctuation characters within the array, those characters can be replaced using strpbrk
in a loop.
#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);
}