Write a program that will allow a list of words to be entered separated by commas,
and then extract the words and output them one to a line, removing any leading or trailing
spaces.
For example, if the input isJohn , Jack , Jill
then the output will beJohn
Jack
Jill
Difficulty level
This exercise is mostly suitable for students
#include <stdio.h>
#include <string.h>
void main(void)
{
char list[5000]; /* Stores the list of comma separated words */
char words[500][20]; /* Array for 500 words of up to 20 characters */
const char comma = ',';
const char space = ' ';
int word_count = 0; /* Number of words found */
int word_length = 0; /* Current word length */
int index = 0; /* Character position */
printf("Enter a comma separated list of words:\n");
gets(list); /* Read the list of words */
/* Find words in list */
while(list[index] != '\0')
{
/* Skip over spaces and commas */
while(list[index] == space || list[index] == comma)
++index;
/* Copy characters that are not space, comma or \0 as part of a word */
while(list[index] != space && list[index] != comma && list[index] != '\0')
words[word_count][word_length++] = list[index++];
words[word_count++][word_length] = '\0'; /* Append terminator */
word_length = 0; /* Reset ready for next word */
}
/* List the words that were found */
printf("\nThe words in the list are:\n");
for(index = 0 ; index<word_count ; index++)
printf("%s\n",words[index]);
}
Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Star pattern 22