-1
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

mch
  • 9,424
  • 2
  • 28
  • 42
Kami
  • 47
  • 5
  • 1
    Please start indenting your code. – Jabberwocky Feb 21 '17 at 11:07
  • 1
    And don't call your variables `array3`, but rather some significant name such as `punctuations`. – Jabberwocky Feb 21 '17 at 11:08
  • 2
    There is too little code, so there is no way for us to find out what could possibly be wrong here. But `strcmp(array3, word)` looks fishy. Turn on compiler warnings and treat warnings as errors. – Jabberwocky Feb 21 '17 at 11:11
  • ... and using an array such this one: `{"a.", "b.", "c.", "d."`, ...}` looks as very bad design anyway. – Jabberwocky Feb 21 '17 at 12:35
  • 1
    @xing Thank you soooo sooo much! I was stuck with this stuff for a weeks! I know it seems to be easy and silly, but I am totally new to the C and I just started having it in uni and I swear, there was nothing about the strcspn . Thank you so, so much again :) – Kami Feb 21 '17 at 22:46

1 Answers1

0

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);
}
xing
  • 2,125
  • 2
  • 14
  • 10