Suppose the user wants to input a string. The length of the string is unknown. I also don't want a character array of size 1000 or what so ever. I want the size of the character array increasing by 1 byte for every character entered. Here is a sample output :

Enter String         : ABCDEFGH

Length of the String : 8

Size of the String   : 8 bytes

Some other conditions

  • The whole string will be entered at once. Not "one character entered, hit enter, then next character"
  • Terminating condition : newline

I was thinking of the algorithm. It could be something like this

int infinite_input()
{
	do
	{
		create new address for character

		if(new address == NULL)
			return 0;		            // denoting failure of function

		input character

		store character at new address

		if(previous address != NULL)
            previous address will point to new address

		echo character			        // if input does not echo

		store new address in 'previous'	// make new address as previous address

		increase size of string by 1

	}while(character != '\n');
	
	point new address to NULL

	return size of string;
}

My main problem is with the input

Recommended Answers

All 2 Replies

You could maybe do it by building a linked list of character variables?
Another approach is each time allocating the memory needed for the current string plus 1 and copying your string to the newly allocated memory (and deallocating the previously used memory afterwards), but it is very inefficient for large strings.
For getting input, you could use the getchar() function for each time getting a character from the input stream, until a newline character is read.

>You could maybe do it by building a linked list of character variables?
That's a little excessive both in terms of space and time. Let's assume you use pointer links for the nodes, that adds the size of a pointer to each character in the "string". It also adds the cost of allocating a new node for each character and whatever overhead is involved in appending the node to the list.

>Another approach is each time allocating the memory needed for the current string plus 1
This is the naive approach. String libraries will typically increase the size by either a chunk or a calculated amount (such as size * 2 ) to avoid expensive memory allocations. For example:

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

char *getline ( FILE *in )
{
#define CHUNK 16

  char *base = NULL;
  size_t capacity = 0;
  size_t size = 0;
  int ch;

  while ( ( ch = fgetc ( in ) ) != EOF ) {
    if ( size == capacity ) {
      char *save = realloc ( base, capacity += CHUNK + 1 );

      if ( save == NULL ) {
        /* If realloc fails, call it a bust and clean up */
        free ( base );
        base = NULL;

#ifdef ENOMEM
        /* Signal a useful error aside from NULL */
        errno = ENOMEM;
#endif

        break;
      }

      base = save;
    }

    if ( ch == '\n' )
      break;

    base[size++] = (char)ch;
  }

  if ( base != NULL )
    base[size] = '\0';

  return base;

#undef CHUNK
}
commented: Good thing that you told him about the downsides as well :) +6
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.