字符串数组中的单词条目

时间:2014-04-04 16:45:18

标签: c arrays string

我试图创建一个程序,该程序最多可读取用户输入的20个单词并存储在字符串数组中。程序将要求输入其他单词,直到输入20个单词或直到单词“完成”为止。已进入。这个想法是将这些单词输入矩阵以创建单词搜索程序。我坚持扫描用户输入的字词。我是一名新程序员,所以任何建议都非常有益。

#include <stdio.h>

int main(void)
{
  char string[20][100];

  printf("Enter up to 20 words to hide in the puzzle.\n");

  printf("Enter the word 'done' after your last word if entering less than 20    words.\n");
  scanf("%s\n",c)

  printf("Entered words:\n");




  return 0;
}

4 个答案:

答案 0 :(得分:1)

#include<stdio.h>
#include<string.h>
int main(void)
{
    char words[20][100];
    char temp[100]="\0";
    int i=0;
    int end=0;  //0 false and 1 true
    printf("Enter 20 words or enter done to exit.\n");

    while(i <=19 && end==0) 
    {
        strset(temp,'\0');// resets array temp to NULL's everytime

        scanf(" %99[^\n]",temp); //this is scan set, to read a string without '\n'
        printf("Given:%s\n\n",temp);

        if(strcmpi(temp,"done")==0)//compares given input with "done".if "done" is entered. zero is returned
            end=1;//when 0 is returned this end=1 will break the loop.
        else//if input is not given "done" then copy temp array to words[i].
        {
            strcpy(words[i],temp);
            i++;
        }
    }
}

我没有直接使用字[20] [100] ,而是使用名为 temp 的临时数组来初始存储输入,因为最后我不会&# 39;想要存储&#34;完成&#34;进入单词[20] [100] 。假设&#34;完成&#34;仅用于结束输入过程,它不是要存储的实际单词。但是您可以根据需要更改此程序。

答案 1 :(得分:0)

我想你希望如下:

#include<stdio.h>

int main(void)
{
    char words[20][100];
    int i = 0;

    printf("Enter up to 20 words to hide in the puzzle.\n");

    printf("Enter the word 'done' after your last word if entering less than 20 words.\n");
    while (i < 20) {
        printf("Entered words:\n");
        if (scanf("%99s", words[i]) != 1 || strcmp(words[i], "done") == 0)
            break;
        printf("%s\n", words[i]);
        i++;
    }
    return 0;
}

答案 2 :(得分:0)

您可以动态分配内存来存储单词。如果用户输入的字数超过20个,则可以使用realloc函数分配更多内存。

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

int main(void) {
    int wlimit = 20;
    int wcount = 0;
    int retval;  // to save the return value of scanf
    char wstring[100+1];  // +1 for the terminating null byte
    char **wlist = malloc(wlimit * sizeof *wlist);

    if(wlist == NULL) {
        // handle it
        printf("not enough memory to allocate\n");
        return 1;
    }
    char *temp;

    while(1) {
        if(wcount >= wlimit) {
            wlimit *= 2;
            temp = wlist;
            wlist = realloc(wlist, wlimit);
            if(temp == NULL) {
                printf("not enough memory to allocate\n");
                wlist = temp; 
            }
        }
        retval = scanf("%100s", wstring);

        // if the input string is done, then break out of the
        // loop else keep taking input from the user
        if(retval != 1 || strcmp(wstring, "done") == 0)
            break;

        // strdup function creates a new string which is a duplicate
        // of the input string and returns a pointer to it which can
        // be freed using free
        wlist[wcount++] = strdup(wstring);
    }

    // do stuff with wlist
    // after done, free the memory
    for(int i = 0; i < wcount; i++)
        free(wlist[i]);

    free(wlist);
    wlist = NULL;

    // stuff

    return 0;
}

答案 3 :(得分:0)

这是developer3466402在其answer中提供的代码的直接但错误修复的变体。

我使用了for循环而不是while循环,因为它整齐地总结了while循环中的操作。我添加了n来记录输入了多少个单词,将i留作循环控制变量(是的,很久以前我也写过Fortran)。

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

int main(void)
{
    char words[20][100];
    int i = 0;
    int n;

    printf("Enter up to 20 words to hide in the puzzle.\n");
    printf("Enter the word 'done' after your last word if entering less than 20 words.\n");
    for (i = 0; i < 20; i++)
    {
        printf("Enter word %2d:\n", i+1);
        if (scanf("%99s", words[i]) != 1 || strcmp(words[i], "done") == 0)
            break;
    }
    n = i;
    printf("%d words entered\n", n);
    for (i = 0; i < n; i++)
        printf("Word %2d = [%s]\n", i+1, words[i]);
    return 0;
}

我可以输入0,1,20和20个单词。我们可以在提示后讨论换行符;它有利有弊,但这一般都是正确的。我选择在循环之后而不是循环内回显数据。请注意,对于某些程序,循环中的回显似乎可以在循环显示问题后回显。

示例运行(提前退出):

$  ./rw
Enter up to 20 words to hide in the puzzle.
Enter the word 'done' after your last word if entering less than 20 words.
Enter word  1:
aleph
Enter word  2:
null
Enter word  3:
absolute
Enter word  4:
twaddle and nonsense
Enter word  5:
Enter word  6:
Enter word  7:
elephants are done for
Enter word  8:
Enter word  9:
8 words entered
Word  1 = [aleph]
Word  2 = [null]
Word  3 = [absolute]
Word  4 = [twaddle]
Word  5 = [and]
Word  6 = [nonsense]
Word  7 = [elephants]
Word  8 = [are]
$