2017-02-21 18 views
-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); 
} 

這是我的拼寫檢查程序代碼,至少是我帶來的大部分問題的一部分。我的程序很好地拼寫檢查任何文本文件,通過比較它與Dicitonary。此代碼中的單詞保留用於包含來自文本文件的錯誤單詞的數組。數組3是包含標點符號的單詞數組,並且看起來像這樣:char* array3[] = {"a.", "b.", "c.", "d.", "e.", "f.", "g.", "h."};我嘗試將單詞與此數組進行比較以擺脫標點符號(在本例中是點,但稍後我計劃了標點符號的其餘部分)。問題是,如果我的數組看起來像「。」,「,」,「!」,「?」,「;」,那麼strcmp只是跳過它,並沒有擺脫標點符號。而且我知道我的方法非常簡單並且不太合適,但是當我用「c。」來嘗試時,它正在工作。另外,我是很新的c語言如何擺脫c拼寫檢查程序中的標點符號?

如果ayone能夠幫助,我會很感激的是,怎麼把我真的堅持這個問題的一個星期了

+1

請開始縮進你的代碼。 –

+1

而不要調用你的變量'array3',而是一些重要的名字,比如'punctuations'。 –

+2

代碼太少,所以我們無法找到可能出錯的地方。但'strcmp(array3,word)'看起來很腥。打開編譯器警告並將警告視爲錯誤。 –

回答

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); 
}