2014-09-25 156 views
1

我想要取一個char *,它代表一個單詞,我從函數中獲得了char *,並將其放入一個2d數組中,但它只是重複第一個每個字的字母數次。輸出看起來像這樣如何將一個char *轉換成一個二維char數組

ttttttttttttttttttttttttttttttttvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvoooooooooooooooooooooooooo 

和我的輸入文件是

three 
versions 
of 
simple 
with 
spell 
check 
checking 
program 
we 
will 
write 

我不知道如何正確地傳遞一個char *爲二維數組 這裏是我使用

的所有功能
#include <stdio.h> 
#include <stdlib.h> 
#include <stdbool.h> 
#include <ctype.h> 
#include "dict.h" 



bool is_valid_entry(int strLength, char entry[]){ 

    if(entry[0] != '"' && !isalpha(entry[0]) && entry[0] != '\'' ){ 
     return false; 
    } 

    strLength--; /* to allow for the zero index of the array*/ 

    for (int i=1; i < strLength; i++) 
     if (!isalpha(entry[i])){ 

      return false; 
     } 

    if (!isalpha(entry[strLength]) && !ispunct(entry[strLength]) && entry[strLength] != '"' && entry[strLength] != '\''){ 

     return false; 
    } 
return true;/* if the entry passes all of the tests the function will return true meaning the the word is valid*/ 

}/* ends is_valid_entry(int strlength, char entry[])*/ 



char * normalize(int strLength, char entry[], char output[]){ 

    strLength--;/* to allow for the zero index of an array*/ 
    int j = 0; 

    for(int i = 0;i < strLength; i++){/* converts all of the elements in the entry[] to lower case*/ 
     if (isupper(entry[i])){ 
      entry[i] = tolower(entry[i]); 
     } 
    } 

    if((entry[0] == '"' && entry[strLength] == '"') || (entry[0] == '\'' && entry[strLength] == '\'')) { 
     for(int i=1 ; i < strLength ; i++,j++){ 
      output[j] = entry[i]; 
     } 
     output[j] = '\0';/* removes the last character which is either a '"' ir a '\''*/ 
     return output;/* returns the noramlized word*/ 

     }else if(entry[0] == '"' || entry[0] == '\''){ 
      for(int i = 1; j < strLength; i++, j++){/*i=1 in order to skip the first element in the entry arrary*/ 
       output[j] = entry[i]; 
      } 
      output[j] = '\0'; 
      return output;/* returns the noramlized word*/ 

     } else if(entry[strLength] == '"' || ispunct(entry[strLength]) || entry[strLength] == '\''){ 
       for(int i = 0;j < strLength; i++,j++){ 
        output[j] = entry[i]; 
       } 
       output[j] = '\0'; 
       return output;/* returns the noramlized word*/ 
      } 


return entry;/* returns the original array since it does not need to be normalized*/ 

}/* ends normalize(int strlength, char entry[], char output[])*/ 
    void load_dict(char *fileName, char dictionary[30000][31]) { 

    FILE *fdict; 
    fdict = fopen(fileName, "r");/* file pointer for the dictionary file*/ 
    char *normDictWord; 
    char normDict [33]; 

    int strLength,i,j,ch;  
    if (fdict == NULL){ 
     fprintf(stderr,"Could not open file: %s\n", fileName);/*checks to make sure the dictionary file can be opened*/ 
     exit(1); 
    } 

    for (i = 0; (ch = fgetc(fdict)) != EOF; i++) {   

     char word[33] = "";/* resets the array*/ 


     for (strLength = 0; !isspace(ch) ; strLength++){ 
      word[strLength] = ch; 
      ch = fgetc(fdict);    
       } 

     if (is_valid_entry(strLength , word)){ 
      normDictWord = normalize(strLength , word , normDict);/*normalize then do the linear search then print the word if it is not found in the dictionary*/ 

      for(j = 0; j <= 31;j++){ 
       dictionary[i][j] = * normDictWord ; 
       printf("%c",dictionary[i][j]); 
      } 

     } 


    } 
    fclose(fdict); 
} 
+0

你的意思是你想把'char *'放在'1-D數組'中嗎? – chouaib 2014-09-25 01:46:03

回答

1

我不能完全理解你的代碼,修復格式,並添加一些缺少的部分(否則我無法測試它)。但是:

dictionary[i][j] = * normDictWord; 

應該是這樣的:

dictionary[i][j] = * (normDictWord + j); 

或等價:

dictionary[i][j] = normDictWord[j] ; 
+0

謝謝,但現在它只會打印一個單詞。爲什麼我不增加? – Loststudent1 2014-09-25 00:37:45

+0

你確定我不會增加嗎?嘗試打印它。 – pez 2014-09-25 00:55:49

1

關鍵問題(@pez)

for (j = 0; j <= 31; j++) { 
    // dictionary[i][j] = *normDictWord; 
    dictionary[i][j] = normDictWord[j]; 
    printf("%c", dictionary[i][j]); 
    } 

其他一些問題:

如果EOF條件發生在空間之前,原始代碼將無限循環。 isspace(EOF)是錯誤的。這個循環也不會阻止溢出。

char word[33] = ""; 

// for (strLength = 0; !isspace(ch); strLength++) { 
for (strLength = 0; !isspace(ch) && ch != EOF && strLength < sizeof word; strLength++) { 
    word[strLength] = ch; 
    ch = fgetc(fdict); 
} 

下列不「將所有的條目[]爲小寫的元素」,只有ellements [0 ]... [original_string_length-2]。也許代碼應該在之後做strLength--;循環。

strLength--;/* to allow for the zero index of an array*/ 
... 
for (int i = 0; i < strLength; i++) {/* */ 
    ...