Re: taking a "word" as input



..> On Tue, 29 Apr 2008 02:47:18 -0700, Nick Keighley wrote:


are all your words the same size?

It depends on the user, what he likes to input at run-time.


If you use the array of pointers you'll have to get the memory
for each word from somewhere (eg. malloc())

yes. I came up with this code and as you can see it does not do what I
want. I want to take every word into the input but it only takes 1st for
obvious reasons. I am not able to think of the way to take all the words
of the input:




#include <stdio.h>
#include <ctype.h>


enum MAXSIZE { MAXWORD = 100 };

char *getword( char *, int );


int main(void) {

char buffer[MAXWORD];

getword( buffer, MAXWORD );

printf("--------------------\n");
printf("%s\n", buffer);

return 0;
}



char *getword( char *word, int max )
{
int c, i;

i = 0;

while( isalpha(c = getchar()) && i < max - 1 )
{
word[i++] = c;
}

word[i] = '\0';

return word;
}

============= OUTPUT =================
/home/arnuld/programs/C $ gcc -ansi -pedantic -Wall -Wextra test.c
/home/arnuld/programs/C $ ./a.out
like that
--------------------
like
/home/arnuld/programs/C $



--
http://lispmachine.wordpress.com/
my email ID is at the above address

.



Relevant Pages

  • Re: Cannot return values of char variable
    ... - buffer = ... Since you seem to be trying to return a char pointer ... int id = random; ... content is interpreted as a string. ...
    (comp.lang.c)
  • Re: doubly-linked list & sorting
    ... have rewritten myself is the getword function. ... ordered manner unless it is a very special hash (e.g. hash(int) = int). ... int getword(char *, int); ... void print_tree(struct tnode *); ...
    (comp.lang.c)
  • [KGDB PATCH][2/7] Serial updates, take 2
    ... Also make put_packet look at the char it reads, ... * Empty the receive buffer first, then look at the interface hardware. ... * This is the receiver interrupt routine for the GDB stub. ... -extern volatile int kgdb_connected; ...
    (Linux-Kernel)
  • Re: Write to file
    ... fwrite, outfile); ... This will repeatedly try to print the first character in buffer as ... int main(int argc, char *argv) ...
    (comp.lang.c)
  • Re: Write to file
    ... int main(int argc, char *argv) ... I intended you you use c here not read another character! ... You reuse the buffer. ...
    (comp.lang.c)