A
arnuld
I have a function named getword that read every single input from std.
input.
WHAT I WANTED: I want it read the word if it has less than or equal to 30
characters. Anything else beyond that should be discarded and it should
ask the user for new input. Ctrl-D should exit from the program.
WHAT I GOT: It reads anything beyond 30 characters as the next word :\ and
I have to press Ctrl-D two times to exit from the program.
/* This program will simply create an array of pointers to integers
* and will fill it with some values while using malloc to create
* pointers to fill the array and then will print the values pointed
* by those pointers
*
* version 1.1
*
*/
#include <stdio.h>
#include <ctype.h>
enum { MAX = 30 };
int getword( char *, int );
int main( void )
{
char word[MAX];
while( getword( word, MAX ) )
{
printf( "----------> %s\n", word );
}
return 0;
}
/* A program that takes a single word as input. It will discard
* the whole input if it contains anything other than the 26 alphabets
* of English. If the input word contains more than 30 letters then only
* the extra letters will be discarded . For general purpose usage of
* English it does not make any sense to use a word larger than this size.
* Nearly every general purpose word can be expressed in a word with less
* than or equal to 30 letters.
*
*/
int getword( char *word, int max_length )
{
int c;
char *w = word;
while( isspace( c = getchar() ) )
{
;
}
if( isalpha( c ) )
{
*w++ = c;
}
while( --max_length && (c = getchar()) )
{
if( isalpha( c ) )
{
*w++ = c;
}
else if( isspace( c ) || c == EOF)
{
*w = '\0';
break;
}
else
{
return 0;
}
}
*w = '\0';
return word[0];
}
================ OUTPUT =====================
[arnuld@raj C]$ gcc -ansi -pedantic -Wall -Wextra test.c
[arnuld@raj C]$ ./a.out
like
----------> like
this
----------> this
lllllllllllllllpppppppppppppqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqpppppppppppdddddddddddddddddddddddddd
----------> lllllllllllllllpppppppppppppqq
----------> qqqqqqqqqqqqqqqqqqqqqqqqqqqqqq
----------> qqqqqqqqqpppppppppppdddddddddd
----------> dddddddddddddddd
[arnuld@raj C]$
input.
WHAT I WANTED: I want it read the word if it has less than or equal to 30
characters. Anything else beyond that should be discarded and it should
ask the user for new input. Ctrl-D should exit from the program.
WHAT I GOT: It reads anything beyond 30 characters as the next word :\ and
I have to press Ctrl-D two times to exit from the program.
/* This program will simply create an array of pointers to integers
* and will fill it with some values while using malloc to create
* pointers to fill the array and then will print the values pointed
* by those pointers
*
* version 1.1
*
*/
#include <stdio.h>
#include <ctype.h>
enum { MAX = 30 };
int getword( char *, int );
int main( void )
{
char word[MAX];
while( getword( word, MAX ) )
{
printf( "----------> %s\n", word );
}
return 0;
}
/* A program that takes a single word as input. It will discard
* the whole input if it contains anything other than the 26 alphabets
* of English. If the input word contains more than 30 letters then only
* the extra letters will be discarded . For general purpose usage of
* English it does not make any sense to use a word larger than this size.
* Nearly every general purpose word can be expressed in a word with less
* than or equal to 30 letters.
*
*/
int getword( char *word, int max_length )
{
int c;
char *w = word;
while( isspace( c = getchar() ) )
{
;
}
if( isalpha( c ) )
{
*w++ = c;
}
while( --max_length && (c = getchar()) )
{
if( isalpha( c ) )
{
*w++ = c;
}
else if( isspace( c ) || c == EOF)
{
*w = '\0';
break;
}
else
{
return 0;
}
}
*w = '\0';
return word[0];
}
================ OUTPUT =====================
[arnuld@raj C]$ gcc -ansi -pedantic -Wall -Wextra test.c
[arnuld@raj C]$ ./a.out
like
----------> like
this
----------> this
lllllllllllllllpppppppppppppqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqpppppppppppdddddddddddddddddddddddddd
----------> lllllllllllllllpppppppppppppqq
----------> qqqqqqqqqqqqqqqqqqqqqqqqqqqqqq
----------> qqqqqqqqqpppppppppppdddddddddd
----------> dddddddddddddddd
[arnuld@raj C]$