-1

User has to input a string of unknown length(<1000). So here I am using while loop, #define, getchar.
What should I do to store the characters simultaneously?

#include <stdio.h>
#define EOL '\n'

int main()
{
    int count=0;
    char c;

    while((c=getchar())!= EOL)// These c's have to be stored.
    {
        count++;
    }

    return 0;
}

EDIT: Sorry I didn't tell this thing earlier. I don't have to waste 1000 bytes if count!=1000. That's why I am not declaring any array of 1000 elements initially.

Tlacenka
  • 520
  • 9
  • 15
Sunil Kumar
  • 390
  • 1
  • 7
  • 25
  • 1
    The quickest and easiest solution, define an array of 1000 elements and use that to store, but otherwise, you can use pointers and realloc() the memory as you go. – Sourav Ghosh Aug 12 '15 at 13:35
  • 2
    You could declare an array of size 1000 and store the input in it using any input methods (`fgets`,`scanf` etc). Or use `getline` if available. Or `malloc`+`realloc`. – Spikatrix Aug 12 '15 at 13:37
  • @CoolGuy What about `fgets()` and `scanf()` here? Aren't we already having the input value in `c` and a simple assignment will do? – Sourav Ghosh Aug 12 '15 at 13:39
  • @SouravGhosh Sure. That will also do. – Spikatrix Aug 12 '15 at 13:58
  • 1
    Btw: `c` should be `int` instead of `char`. – pzaenger Aug 12 '15 at 14:38

2 Answers2

1

If you don't know exactly how many characters will be in the string, but have a hard upper limit, you can simply allocate enough space for that upper limit:

char tmpArray[1000];

Store the characters in there:

while((c=getchar())!=EOL)
{
    tmpArray[count] = c;
    count++;
}

and then after your loop finishes and you know how many characters there are (via count variable), you can allocate a new array with the correct amount and copy the temp string into it:

char actualArray[count];
for(int i = 0;i < count + 1;i++) {
    actualArray[i] = tmpArray[i];
}

However this is not great as the large array cannot be freed up/removed from memory. It is probably a better idea to use malloc and a char* to do this:

char* tmpArray = malloc((sizeof(char)) * 1001);
while((c=getchar())!=EOL) {
    tmpArray[count] = c;
    count++;
}

char* actualArray = malloc((sizeof(char)) * count + 1);
strncpy(actualArray, tmpArray, count + 1);
free(tmpArray);

/***later in the program, once you are done with array***/
free(actualArray);

strncpy's arguments are (destination, source, num) where num is the number of characters to be transferred. We add one to count so that the null terminator at the end of the string is transferred as well.

Spikatrix
  • 20,225
  • 7
  • 37
  • 83
Caden Depatie
  • 126
  • 1
  • 4
  • `for(int i = 0;i < count + 1;i++)` should be `for(int i = 0; i < count; i++)`. The same goes for the `malloc` version. Why? Because there isn't a NUL-terminator (because input is taken one by one via `getchar` until `EOL`). You need to add that manually. – Spikatrix Aug 12 '15 at 14:00
1

Implement a dynamically growing array

  1. use calloc() or malloc() to allocate a small memory block - let's say for 10 chars

  2. use realloc() to increase the size of the memory block if needed

Example:

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

#define EOL '\n'

int main()
{
    int count = 0;
    int size = 10;
    char *chars = NULL;
    char c;
    
    /* allocate memory for 10 chars */
    chars = calloc(size, sizeof(c));
    if(chars == NULL) {
        printf("allocating memory failed!\n");
        return EXIT_FAILURE;
    }

    /* read chars ... */
    while((c = getchar()) != EOL) {
        /* re-allocate memory for another 10 chars if needed */
        if (count >= size) {
            size += size;
            chars = realloc(chars, size * sizeof(c));
            if(chars == NULL) {
                printf("re-allocating memory failed!\n");
                return EXIT_FAILURE;
            }
        }
        chars[count++] = c;
    }

    printf("chars: %s\n", chars);

    return EXIT_SUCCESS;
}
Community
  • 1
  • 1
sergej
  • 17,147
  • 6
  • 52
  • 89