简体   繁体   English

需要知道如何在 c 中按空格解析单词。 还需要知道我是否正确分配内存?

[英]Need to know how to parse words by space in c. Also need to know if I am allocating memory correctly?

I am writing a program in c that reads in text from a text file then randomly selects words from the file and if the words are greater than or equal to six it appends the words together, removes the spaces, and finally prints the new word.我正在用 c 编写一个程序,它从文本文件中读取文本,然后从文件中随机选择单词,如果单词大于或等于 6,则将单词附加在一起,删除空格,最后打印新单词。 (I am using the redirect on linux "<" to read in the file) (我在 linux 上使用重定向“<”来读入文件)

 Example input: "cheese and crackers" New word should be: cheesecrackers

Here is the code:这是代码:

int main (void)
{
    int ch;
    char *ptrChFromFile;
    int strSize = 1;
    int i;
    int numberOfWords = 1;

    ptrChFromFile = malloc (sizeof (char));

    if (ptrChFromFile == NULL) {
        puts ("COULDN'T ALLOICATE MEMORY");
        exit (EXIT_FAILURE);
    }

    while ((ch = getchar ()) != EOF) {
        ptrChFromFile =
            realloc (ptrChFromFile, (strSize + 1) * sizeof (char));

        if (ptrChFromFile == NULL) {
            puts ("failed to allocate memory");
            exit (EXIT_FAILURE);
        }

        if (ch == ' ') {
            numberOfWords++;
        }

        ptrChFromFile[strSize] = ch;
        strSize++;
    }

    ptrChFromFile[strSize] = 0;

    char **ptrWords = malloc (sizeof (char *) * strSize);


    for (i = 0; i < strSize; i++) {
        if (ptrChFromFile[i] != ' ') {
            ptrWords[i] = &ptrChFromFile[i];
        }
        else {
            ptrWords[i] = 0;
        }
    }

    free (ptrChFromFile);
    free (ptrWords);
    return 0;
}

The things that I am struggling with are:我正在努力解决的问题是:

1) Am I allocating the correct memory size for the pointers? 1)我是否为指针分配了正确的内存大小?

2) How can I parse each word by space without using any special methods from the string.h library (like strtok). 2)如何在不使用 string.h 库(如 strtok)中的任何特殊方法的情况下按空格解析每个单词。 Then how do I store those words in the pointer *ptrWords?那么如何将这些单词存储在指针 *ptrWords 中呢?

so ptrWords should look like this:所以 ptrWords 应该是这样的:


cheese |奶酪 | and |和| crackers饼干

 0 1 2

Then I want to loop through ptrWords and check if the length of each word in the pointer is greater than or equal to six.然后我想循环遍历 ptrWords 并检查指针中每个单词的长度是否大于或等于 6。 If they are store them in the pointer ptrOutputWord.如果将它们存储在指针 ptrOutputWord 中。

so then ptrOutputWord should look like this:所以 ptrOutputWord 应该是这样的:


cheese |奶酪 | crackers饼干

 0 1

Finally, I want to print the values in ptrOutputWord as one word without spaces.最后,我想将 ptrOutputWord 中的值打印为一个没有空格的单词。

I tried to explain what I want to do exactly.我试图解释我到底想做什么。 Thank you to anyone that can help in advance.感谢任何可以提前提供帮助的人。

EDIT: I changed the code to reflect only the piece that should read in the characters, and reallocate the size of the pointer by one each time a new character is read in, but the right amount of memory isn't being allocated.编辑:我更改了代码以仅反映应该读入字符的部分,并在每次读入新字符时将指针的大小重新分配一个,但没有分配正确的内存量。

You have a few issues:你有几个问题:

#include <stdio.h>
#include <time.h>

Why this header?为什么是这个标题?

#include <stdlib.h>

int main()
{
  char ch, *ptrChFromFile; 
  int strSize;

This variable needs to have a useful start value.这个变量需要有一个有用的起始值。

  ptrWordsFromFile = (char*)malloc(sizeof(char));

No need to cast.没必要投。

  if(ptrChFromFile == NULL)
  {
     puts("COULDN'T ALLOICATE MEMORY");
     exit(EXIT_FAILURE);
  }

  while((ch = getchar()) != EOF)

getchar returns and int , not a char . getchar返回和int ,而不是char

  {
    ptrChFromFile  = (char*)realloc(ptrChFromFile, strSize * sizeof(char)+1);

We need one more character than before and extra space for the 0 .我们需要比以前多一个字符并为0额外的空间。 You should add the +2 (not +1) to the number of elements: (strSize+2) * sizeof(<any type>)您应该将 +2(而不是 +1)添加到元素数中: (strSize+2) * sizeof(<any type>)

Normally you should not directly assign the result of realloc to the same pointer.通常你不应该直接将realloc的结果分配给同一个指针。 In case it fails, you lose your old pointer value.如果失败,您将丢失旧的指针值。 Again: No cast needed.再次:不需要演员。

    if(ptrChFromFile == NULL)
      {puts("failed to alloicate memory");}

If it fails, you cannot continue!如果失败,您将无法继续! Exit from the program just as above和上面一样退出程序

    *ptrChFromFile = ch;

You put the character to the start of the enlarged buffer.您将字符放在扩大的缓冲区的开头。 You should add at the end.你应该在最后添加。

    strSize++;
  }

Now you have a bunch of characters in memory but no termination for the string.现在您在内存中有一堆字符,但没有终止字符串。

  free(ptrChFromFile);
  return 0;
}

After fixing it it looks like this:修好之后是这样的:

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

int main(void)
{
  int ch;
  char *ptrChFromFile; 
  int strSize = 0;

  ptrWordsFromFile = malloc(sizeof(char));

  if (ptrChFromFile == NULL)
  {
     puts("COULDN'T ALLOICATE MEMORY");
     exit(EXIT_FAILURE);
  }

  while ((ch = getchar()) != EOF)
  {
    ptrChFromFile = realloc(ptrChFromFile, (strSize+2) * sizeof(char));

    if (ptrChFromFile == NULL)
    {
      puts("failed to allocate memory");
      exit(EXIT_FAILURE);
    }

    ptrChFromFile[strSize] = ch;
    strSize++;
  }
  ptrChFromFile[strSize] = 0;

  // Now add detection and storing of separate words
  // (You might omit storing words that are too short)
  // Select random words and add together.

  free(ptrChFromFile);
  return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM