2015-05-16 28 views
2

我的任務是允許用戶輸入任何輸入並打印出字母和單詞,我們還必須打印出多少個一個字母,兩個,三個字母等字母。我已經讓我的代碼中的部分代碼工作並且多次修改了我的單詞功能,但仍然無法使單詞查找功能開始工作。編譯器說,當它清楚時,char指針字未聲明。我是否必須爲其分配內存和字符數組?如何分配內存到我的字符指針?

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 


void findLetters(char *ptr); 
void findWords(char *point); 


int main() 
{ 
    char textStream[100]; //up to 98 characters and '\n\ and '\0' 

    printf("enter some text\n"); 
    if (fgets(textStream, sizeof (textStream), stdin)) //input up to 99 characters 
    { 
     findLetters(textStream); 
     findWords(textStream); 
    } 
    else 
    { 
     printf("fgets failed\n"); 
    } 

    return 0; 
} 

void findLetters(char *ptr) //find occurences of all letters 
{ 
    int upLetters[26]; 
    int loLetters[26]; 
    int i; 
    int index; 

    for (i = 0; i < 26; i++) // set array to all zero 
    { 
     upLetters[i] = 0; 
     loLetters[i] = 0; 
    } 
    i = 0; 
    while (ptr[i] != '\0') // loop until prt[i] is '\0' 
    { 
     if (ptr[i] >= 'A' && ptr[i] <= 'Z') //stores occurrences of uppercase letters 
     { 
      index = ptr[i] - 'A';// subtract 'A' to get index 0-25 
      upLetters[index]++;//add one 
     } 

     if (ptr[i] >= 'a' && ptr[i] <= 'z') //stores occurrences of lowercase letters 
     { 
      index = ptr[i] - 'a';//subtract 'a' to get index 0-25 
      loLetters[index]++;//add one 
     } 
     i++;//next character in ptr 
    } 
    printf("Number of Occurrences of Uppercase letters\n\n"); 
    for (i = 0; i < 26; i++)//loop through 0 to 25 
    { 
     if (upLetters[i] > 0) 
     { 
      printf("%c : \t%d\n", (char)(i + 'A'), upLetters[i]); 
      // add 'A' to go from an index back to a character 
     } 
    } 
    printf("\n"); 
    printf("Number of Occurrences of Lowercase letters\n\n"); 
    for (i = 0; i < 26; i++) 
    { 
     if (loLetters[i] > 0) 
     { 
      printf("%c : \t%d\n", (char)(i + 'a'), loLetters[i]); 
      // add 'a' to go back from an index to a character 
     } 
    } 
    printf("\n"); 
} 

void findWords(char *point) 
{ 
    int i = 0; 
    int k = 0; 
    int count = 0; 
    int j = 0; 
    int space = 0; 
    int c = 0; 
    char *word[50]; 
    char word1[50][100]; 
    char* delim = "{ } . , () "; 

    for (i = 0; i< sizeof(point); i++) //counts # of spaces between words 
    { 
     if ((point[i] == ' ') || (point[i] == ',') || (point[i] == '.')) 
     { 
      space++; 
     } 
    } 
    char *words = strtok(point, delim); 
    for(;k <= space; k++) 
    { 
     word[k] = malloc((words+1) * sizeof(*words)); 
    } 

     while (words != NULL) 
     { 
      printf("%s\n",words); 
      strcpy(words, word[j++]); 
      words = strtok(NULL, delim); 
     } 

    free(words); 
} 
+3

自由,你認爲這將有可能歸結您的內存分配問題分解成較小的程序?對於每個人來說,更容易進行調試,而不用所有的字母計數的東西(當你試圖簡化它時,你甚至可能會發現什麼是錯的!) – hugomg

回答

1

你有一對夫婦的問題在功能findWords

  1. 這裏,

    for (i = 0; i< sizeof(point); i++) 
    

    sizeof(point)與功能fincdWords中的char*中的sizeof(char*)相同爲point。這不是你想要的。使用

    for (i = 0; i < strlen(point); i++) 
    

    改爲。但是這可能會很慢,因爲在每次迭代中都會調用strlen。因此,我建議

    int len = strlen(point); 
    for (i = 0; i < len; i++) 
    
  2. 同樣的問題也出在這裏:

    word[k] = malloc((words+1) * sizeof(*words)); 
    

    它不有道理你與(words+1)努力。我想你想

    word[k] = malloc(strlen(words) + 1); //+1 for the NUL-terminator 
    
  3. 你得到了所有的參數混合起來:

    strcpy(words, word[j++]); 
    

    你其實是想

    strcpy(word[j++], words); 
    

    其副本words的內容word[j++]

  4. 這裏:

    free(words); 
    

    words從未分配的內存。由於您釋放了未由malloc/calloc/realloc返回的指針,因此代碼顯示未定義的行爲。所以,刪除它。 您爲word的每個元素分配了內存。因此,它使用

    for(k = 0; k <= space; k++) 
    { 
        free(word[k]); 
    } 
    
+0

我對我的代碼進行了修改,並且更多和前面的所有錯誤都消失了,但現在我在strcpy(word [k],words)寫入位置時出現訪問衝突。 – Karlioh

+0

請不要更改整個代碼。它使答案和意見不完整。發佈一個提供必要細節的新問題。 –

+0

對不起 – Karlioh

2

這是因爲您試圖將指針位置+ 1乘以指針的大小。將第100行更改爲:

word[k] = malloc(strlen(words)+1); 

這將解決您的編譯問題,但仍存在其他問題。

1

您對指針的位置+ 1的計算錯了。如果你想編譯問題就會變線100走開:

​​