Answers

Strings 
Aim: Understanding and using character-handling, string-conversation and string-processing 
functions. 
 
Exercises: 
1. Write a program that inputs a line of text, tokenizes the line as words and outputs 
tokens in reverse order. 
Example 
Input: C characters and strings 
Output: strings and characters C 
 
2. Write a program that uses random number generation to create sentences. The 
program should use four arrays of pointers to char called article, noun, verb and 
preposition. The program should create a sentence by selecting a word at random from 
each array in the following order: article, noun, verb, preposition, article and noun. As 
each word is picked, it should be concatenated to the previous words in an array large 
enough to hold the entire sentence. The words should be separated by spaces. When 
the final sentence is output, it should start with a capital letter and end with a period. 
The program should generate 20 such sentences. The arrays should be filled as 
follows: The article array should contain the articles "the", "a", "one", "some" and 
"any"; the noun array should contain the nouns "boy", "girl", "dog", "town" and "car"; 
the verb array should contain the verbs "drove", "jumped", "ran", "walked" and 
"skipped"; the preposition array should contain the prepositions "to", "from", "over", 
"under" and "on". 
Example Output: 
1. The girl ran on the dog. 
2. Some girl skipped over the girl. 
3. Any dog ran under any car. 
… 
20. One dog skipped over one car. 
 
3. Write a program that reads a series of strings and prints only those strings that ends 
with letters “ed”. 
Example 
Input: 
Doctor Who is a British science-fiction television programme produced by the BBC. 
The programme depicts the adventures of the Doctor, a Time Lord—a time-travelling 
humanoid alien. 
He explores the universe in his TARDIS, a sentient time-travelling space ship. 
Its exterior appears as a blue British police box, which was a common sight in Britain 
in 1963 when the series first aired. 
Output: Its exterior appears as a blue British police box, which was a common sight 
in Britain in 1963 when the series first aired.