Author Topic: [C] Store word in array  (Read 1445 times)

0 Members and 1 Guest are viewing this topic.

Offline Code.Illusionist

  • Royal Highness
  • ****
  • Posts: 687
  • Cookies: 39
  • Compile or die trying
    • View Profile
[C] Store word in array
« on: May 27, 2013, 12:27:55 pm »
I was wondering, because C have some retarded string manipulation, how to get string from user and store in array?
Vae Victis - suffering to the conquered

Offline s3my0n

  • Knight
  • **
  • Posts: 276
  • Cookies: 58
    • View Profile
    • ::1
Re: [C] Store word in array
« Reply #1 on: May 27, 2013, 03:27:10 pm »
You can do either using predefined MAX_SIZE:
Code: (c) [Select]
char string[MAX_SIZE];

printf("Enter a string: ");
fscanf(string, MAX_SIZE, stdin);

// fscanf also gets the newline character so we do this:
int length = strlen(string);
if (string[length-1] == '\n')
    string[length-1] = '\0';
else {
    printf("You entered a string that was too large to fit in the buffer!\n");
    // clearing the rest of the char that are left from the user input
    while (getchar() != '\n');
}

or you  can allocate space dynamically. Do you want me to show you how? Cause this is mostly fine for most programs.

EDIT:
Almost forgot about clearing the buffer if the whole of user input was not stored.
« Last Edit: May 27, 2013, 03:29:40 pm by s3my0n »
Easter egg in all *nix systems: E(){ E|E& };E

Offline Code.Illusionist

  • Royal Highness
  • ****
  • Posts: 687
  • Cookies: 39
  • Compile or die trying
    • View Profile
Re: [C] Store word in array
« Reply #2 on: May 27, 2013, 03:32:06 pm »
Show me the other way as well.
Vae Victis - suffering to the conquered

Offline Stackprotector

  • Administrator
  • Titan
  • *
  • Posts: 2515
  • Cookies: 205
    • View Profile
Re: [C] Store word in array
« Reply #3 on: May 27, 2013, 03:37:41 pm »
Show me the other way as well.
I think you might want to look into the C programming basics instead of asking separate questions you would have learned otherwise.
~Factionwars

Offline s3my0n

  • Knight
  • **
  • Posts: 276
  • Cookies: 58
    • View Profile
    • ::1
Re: [C] Store word in array
« Reply #4 on: May 27, 2013, 03:56:09 pm »
I think this should work. It's not the most optimal way because of constant reallocs and possible wasted space, but it works and is dynamic. Don't forget to check the return value of fgets() for errors.

Code: (c) [Select]
const unsigned int CHUNK_SIZE = 16; // whatever you want here

char *string = NULL;
int length = 0;

bool done = false;
while (!done) {
    printf("Enter a string: ");
    if (realloc(string, length+CHUNK_SIZE))
        fscanf(string[length], CHUNK_SIZE, stdin);
    else {
        fprintf(stderr, "Out of memory!\n");
        break;
    }
 
    length += strlen(string[length]);
    if (string[length-1] == '\n') {
        string[length-1] = '\0';
        done = true;
    }
}
Easter egg in all *nix systems: E(){ E|E& };E

Offline Code.Illusionist

  • Royal Highness
  • ****
  • Posts: 687
  • Cookies: 39
  • Compile or die trying
    • View Profile
Re: [C] Store word in array
« Reply #5 on: May 27, 2013, 05:32:00 pm »
Thank you s3my0n. :)
Vae Victis - suffering to the conquered