comp.lang.c
[Top] [All Lists]

Re: taking a "word" as input

Subject: Re: taking a "word" as input
From: arnuld
Date: Thu, 01 May 2008 03:02:22 +0500
Newsgroups: comp.lang.c

.> 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


<Prev in Thread] Current Thread [Next in Thread>
Privacy Policy