从C中的指针数组中删除元素

时间:2016-10-10 08:19:50

标签: c arrays pointers

我试图找出为什么我无法从dict数组中删除元素。有人可以帮帮我吗? removeWord函数在删除最后添加的单词时应该正常工作,但在尝试删除其他单词时则不行。

#include <stdio.h>
#include <stdlib.h> 
#include <string.h>   
#define MAX_NUMBER_OF_WORDS 11

int clear(){
    while(getchar()^'\n');
    return 0;
}

int numberOfWordsInDict(char **dict){
    int i = 0;
    int c1 = 0;
    for (i = 0; i < MAX_NUMBER_OF_WORDS; ++i){
        if (dict[i] != 0){
            c1++;
        }
    }
    return c1;
}

void addWord(char **dict, char *word){
    int c1 = numberOfWordsInDict(dict);
    char *word1;
    if (c1 >= 0 && c1 < 10){
        word1 = (char*) malloc(sizeof(char)*(strlen(word)+1));
        dict[c1] = word1;
        strncpy(dict[c1], word, strlen(word));
        dict[c1][strlen(word)] = '\0';
    } else if (c1 >= 10){
        printf("Dictionary is already full!\n");
    }
}

void printDict(char **dict){
    int i = 0;
    int c1 = numberOfWordsInDict(dict);
    printf("Dictionary:\n");
    if (c1 == 0){
        printf("The dictionary is empty.\n");
    } else if (c1 > 0 && c1 <= 10){
        while (dict[i] != NULL){
            printf("- %s\n", dict[i]);
            i++;
        }
    }
}

void removeWord(char **dict, char *word){
    int i = 0;
    for (i = 0; i < MAX_NUMBER_OF_WORDS; i++){
        if (strncmp(dict[i], word, strlen(word)+1) == 0){
            dict[i] = 0;// can only delete the last element of dict properly.
            break;
        }
    }
}

int main(){
    char *dict[MAX_NUMBER_OF_WORDS] = {};
    char word[1024] = {};
    char command;

    while(1){
        printf("Command (a/p/r/q): ");
        while(scanf("%s", &command) == 1){
            break;
        }
        ;
        clear();

        if (command == 'a'){        // add word
            scanf("%[^\n]s", &word);
            clear();
            addWord(dict, word);
        } else if (command == 'p'){     // print dict
            printDict(dict);        
        } else if (command == 'r'){     // remove word
            printf("Remove a word: ");
            scanf("%[^\n]s", &word);
            clear();
            removeWord(dict, word);
        } else if (command == 'q'){     // quit
            break;
        }
    }

    int i = 0;
    for (i = 0; i < MAX_NUMBER_OF_WORDS; i++){
        free(dict[i]);
    }

    return 0;
}

示例输入:

a
dog
a
cat
a
apple

案例1:

r
apple
p
// output =
Dictionary:
- dog
- cat
a
uniform
p
// output =
Dictionary:
- dog
- cat
- uniform
// works fine

案例2

r
cat
p
// output =
Dictionary:
- dog
a
book
p
// output =
Dictionary:
- dog
// doesn't work as expected

2 个答案:

答案 0 :(得分:0)

printDict()内使用

  while (i<MAX_NUMBER_OF_WORDS){  
       if(dict[i] != 0)
           printf("- %s\n", dict[i]);
        i++;
    }

而不是

  while (dict[i] != NULL){
        printf("- %s\n", dict[i]);
        i++;
    }

因为即使您知道是否有c1个字数,您也不知道它们的位置与c1字存在的位置相同。 也改变

  while(scanf("%s", &command) == 1) 

  while(scanf("%c", &command) == 1)

因为command属于char类型。

答案 1 :(得分:0)

#include <stdio.h>
#include <stdlib.h> 
#include <string.h>   
#define MAX_NUMBER_OF_WORDS 11

int clear(){
    while(getchar()^'\n');
    return 0;
}

int numberOfWordsInDict(char **dict){
    int c1 = 0;
    for(int i = 0; i < MAX_NUMBER_OF_WORDS; ++i){
        if (dict[i] != 0){
            c1++;
        }
    }
    return c1;
}

int vacancy(char **dict){
    for(int i = 0; i < MAX_NUMBER_OF_WORDS; ++i){
        if (dict[i] == 0){
            return i;
        }
    }
    return -1;
}

void addWord(char **dict, char *word){
    int c1 = vacancy(dict);//It is not possible to use the registration number as an additional index.

    if (-1 != c1){
        dict[c1] = malloc(strlen(word)+1);
        strcpy(dict[c1], word);
    } else {
        printf("Dictionary is already full!\n");
    }
}

void printDict(char **dict){
    int c1 = numberOfWordsInDict(dict);
    printf("Dictionary:\n");
    if (c1 == 0){
        printf("The dictionary is empty.\n");
    } else {
        for(int i = 0; i < MAX_NUMBER_OF_WORDS; ++i){
            if(dict[i])
                printf("- %s\n", dict[i]);
        }
    }
}

void removeWord(char **dict, char *word){
    for(int i = 0; i < MAX_NUMBER_OF_WORDS; i++){
        if (strcmp(dict[i], word) == 0){
            free(dict[i]);//need free
            dict[i] = 0;// can only delete the last element of dict properly.
            break;
        }
    }
}

int main(){
    char *dict[MAX_NUMBER_OF_WORDS] = { NULL };
    char word[1024] = { 0 };//forbids empty initializer braces
    char command;

    while(1){
        printf("Command (a/p/r/q): ");
        scanf("%c", &command);//%s buffer over run
        clear();

        if (command == 'a'){        // add word
            scanf("%[^\n]", word);
            clear();
            addWord(dict, word);
        } else if (command == 'p'){  // print dict
            printDict(dict);        
        } else if (command == 'r'){  // remove word
            printf("Remove a word: ");
            scanf("%[^\n]", word);
            clear();
            removeWord(dict, word);
        } else if (command == 'q'){  // quit
            break;
        }
    }

    int i = 0;
    for (i = 0; i < MAX_NUMBER_OF_WORDS; i++){
        free(dict[i]);
    }

    return 0;
}