Δημοσιεύτηκε: 24 Μαρ 2012, 03:01
Ηλία είσαι ωραίος
Δίνω και εγώ έναν τρόπο, που αγνοεί και τα σημεία στίξης μαζί με τα κενά...
Δίνω και εγώ έναν τρόπο, που αγνοεί και τα σημεία στίξης μαζί με τα κενά...
- Μορφοποιημένος Κώδικας: Επιλογή όλων
-
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define MAXINPUT (255+1)
#define MAXWORDS 30
#define WLEN (20+1)
#define CHR_ACCEPTABLE(ch) ( !isspace( (int)(ch) ) && !ispunct( (int)(ch) ) )
/* ------------------------------------------------------- */
void print_words( char words[][WLEN] )
{
if ( !words || !*words )
return;
for (int i=0; i < MAXWORDS && *(words[i]) != '\0'; i++ )
puts( words[i] );
return;
}
/* ------------------------------------------------------- */
char *s_cpTrim( const char *s )
{
char *cp = (char *)s;
if ( !s )
return NULL;
while ( !CHR_ACCEPTABLE(*cp) )
cp++;
return cp;
}
/* ------------------------------------------------------- */
int main( void )
{
char input[ MAXINPUT ] = {'\0'};
char words[ MAXWORDS ][ WLEN ] = { {'\0'} };
char *cp = NULL, *ws = NULL; /* ws: char-ptr to start of next word */
int iword = 0;
printf("Type your sentence: ");
fgets( input, MAXINPUT, stdin);
/* trim leading non-word chars */
ws = s_cpTrim( input );
iword = 0;
for (cp=ws; *cp && iword < MAXWORDS; cp++)
{
if ( !CHR_ACCEPTABLE(*cp) ) {
*cp++ = '\0';
strncpy( words[iword++], ws, WLEN-1 );
ws = cp = s_cpTrim( cp );
}
}
print_words( words );
system("pause"); /* windows only */
exit( EXIT_SUCCESS );
}