2016-12-04 70 views
1

我正在从“C编程现代方法第2版”文本中解决问题。我想写一个写最小和最大单词的程序。当用户输入一个4个字母的单词时,程序停止接受输入。C将字符串存储到数组中

我正在使用字符串数组来解决这个问题,但我甚至无法让我的程序在其中存储单词。

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

#define WORD_LEN 20 

int main() 
{ 
    char word[WORD_LEN]={0},ch; 
    char *a[10]={};   //Max 10 words in the array 
    int i=0,j; 

    for(;;) 
    { 
     printf("Enter a word: "); 
     fgets(word,WORD_LEN,stdin); 
     strtok(word, "\n");    //removes newline 

     a[i] = word; 
     if(strlen(word) == 4)   //if word is 4 characters 
      break;      //break out of loop 

     i++; 
    } 

    for(j=0;j<i;j++)      //displaying array 
     printf("%s\n",a[j]); 

    return 0; 
} 

输出:

Enter a word: Analysis 
Enter a word: Martin 
Enter a word: Jonathan 
Enter a word: Dana 
Dana 
Dana 
Dana 

任何想法变成什么,我做错了什么?谢谢。

+2

'A [1] =字;':设置word'的'相同ADDRES到'A [1]' – BLUEPIXY

回答

2

正如BLUEPIXY提到的,您在所有的[i]中都存储相同的地址。所以在循环结束时,它会打印最后一次输出。

解决方案: 您需要为[i]分配内存并复制字符串。

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

#define WORD_LEN 20 
#define MAX_NUM_WORD 10 //Max 10 words in the array 

int main() 
{ 
    char word[WORD_LEN]={0},ch; 
    char *a[MAX_NUM_WORD]={0};    
    int i=0,j; 

    for(;;) 
    { 
     printf("Enter a word: "); 
     fgets(word,WORD_LEN,stdin); 
     strtok(word, "\n");    //removes newline 

     a[i] = malloc(sizeof(char)* (strlen(word)+1)); //1 for '\0' 

     strcpy(a[i], word); 

     i++; 
     if(strlen(word) == 4)   //if word is 4 characters 
      break;      //break out of loop 

     //i++; //You will be missing last 4 letter word if i++ is here. 
     if(MAX_NUM_WORD <= i) //You can store only MAX_NUM_WORD strings 
      break; 
    } 

    for(j=0;j<i;j++)      //displaying array 
     printf("%s\n",a[j]); 

    //Your other code. 

    for(i=0; i<MAX_NUM_WORD && NULL != a[i]; i++) 
     free(a[i]); //Free the allocated memory. 

    return 0; 
} 
1

添加到别人的答案,使用malloc时,为你的字符串分配内存,这是好事,也是检查从它返回void*指针的返回值。

此外,检查返回值fgets也是安全的,只是为了超级安全。

该溶液表明这些点:

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

#define WORD_LEN 20 
#define MAX_NUM_WORD 10 
#define EXIT_LEN 4 

int 
main(void) { 
    char word[WORD_LEN]; 
    char *a[MAX_NUM_WORD]; 
    int i = 0, wrd; 

    while (i < MAX_NUM_WORD) { 
     printf("Enter a word: "); 
     if (fgets(word, WORD_LEN, stdin) != NULL) { 
      word[strlen(word)-1] = '\0'; 
     } 

     a[i] = malloc(strlen(word)+1); 
     if (a[i] == NULL) { 
      fprintf(stderr, "%s\n", "Malloc Problem"); 
      exit(EXIT_FAILURE); 
     } 

     strcpy(a[i], word); 

     i++; 

     if (strlen(word) == EXIT_LEN) { 
      break; 
     } 

    } 

    // Print and free, all at once. 
    for (wrd = 0; wrd < i; wrd++) { 
     printf("%s\n", a[wrd]); 
     free(a[wrd]); 
     a[wrd] = NULL; 
    } 

    return 0; 
}