Dear all,
First of all I must say that I have just started with C. I've been doing some Java and C# as well but not very much of them too.


In my new course of C, which goes on much rapidly than one can catch something, I am given some material teaching pointers, structs, unions, bit structures and pointers to functions. And my exercise is to take an input from the user and store the words in input in an array. (i.e. break the input string from spaces and tabs and store the words).

Coming from Java to C, I don't have a smallest idea of how to do this!!!


Now my problem is how can I store in an array without knowing the lengths previously.
I've been googling for hours and found out that I could use readline() method to take the input. So that problem solved. But the rest, I don't have an idea.


The problem is I don't have a starting point. I don't know what to search for and what kind of a code should I write. All I ask for is a little hint so I could start off. I have all the material but I'm not intelligent enough to attack a problem like this with only reading material. Help me please, I'm desperate. :(

Thanks..

Since you don't know in advance how many words there are, and how long they will be, you need to use dynamic memory allocation:

1. malloc
2. realloc
3. free

There is no readline function in standard C.
There is an fgets function that reads a line of text. You will need to know how long a line can be in advance to use fgets, though.
Failing that, you can use fread and then find the line separators yourself. This is a similar problem to separating the lines into words, so you may be able to reuse some of your code between the two problems.

Edited 6 Years Ago by Martin B: n/a

Here is a way to do it. Convert the TestInput to your scanf implementation. Also not included in this example is a main function, the includes, and the free functions which free the memory created by realloc and calloc.

#define MAX_STRING_SIZE 256

typedef struct { // This structure will store a single line
  char *Line;
} DS;

char TestInput[] = "Hi How Are you?"; // Here is a sample string

DS *Data = NULL; 

int i,         // The index of the current character in the sample string
    new = 1,   // A flag which tells the parser to create a new line if "1"
    lines = 0; // Number of lines created in "Data"

for (i=0; i<strlen(TestInput); i++) 
  switch (TestInput[i]) {
     case ' ':  // ignore spaces
     case '\t': // ignore tabs
     case '\n': // ignore new lines
       new = 1; // remind the parser that the next string will be new
       break;
     default:
       // if this is a new string, prepare a space for it in the array
       if (new==1) {
          // No longer new since we are creating it
          new = 0; 
          // Expand the array's memory for the new line
          Data = realloc(Data, sizeof(DS) * (++lines)); 
          // Create some empty memory for the line string
          Data[0].Line = calloc(MAX_STRING_SIZE);
       }
       // Append this "approved" character to the current line
       strncat(Data[lines-1].Line, &TestInput[i], 1);
       break;
  }
After the loop, Data will contain the follwing:

  Data[0].Line contians "Hi"
  Data[1].Line contians "How"
  Data[2].Line contians "Are"
  Data[3].Line contians "you?"

lines will contain number '4'

Thanks, this might solve my problem, and about that readline, sorry I forgot to tell. It's a package that is allowed to download and use in our course.

This question has already been answered. Start a new discussion instead.