Exercise: Hyphenated Words

Exercise 13 Easy

Prerequisites for the exercise

  1. PHP Strings — String Functions
  2. Python Strings — Basics
  3. All previous chapters

Objective

Extract out individual words from a hyphenated word.

Description

Ask the user to input a hyphenated word by the following input prompt message:

Enter a hyphenated word:

Once the word is given, extract out the constituent words from it, and print each on a new line.

Shown below is an example:

Enter a hyphenated word: awe-inspiring

awe
inspiring

If the input word is not hyphenated i.e. there is not a hyphen (-) in it, print 'Invalid input!', and keep asking the user to enter a hyphenated string, as shown above, until the correct input is received.

Shown below is a demonstration:

Enter a hyphenated word: amazing
Invalid input!

Enter a hyphenated word: programming
Invalid input!

Enter a hyphenated word: strongly-typed

strongly
typed

Note the blank lines over here.

If the input value is invalid, print 'Invalid input!' and then leave a blank line before asking the user to input again. If the input value is valid, leave a blank line before printing out all the constituent words in the hyphenated word.

Hints

Hint 1

Use the explode() string function to extract out the individual words from the given input value.

View Solution

New file

Inside the directory you created for this course on PHP, create a new folder called Exercise-13-Hyphenated-Words and put the .php solution files for this exercise within it.

Solution

The description clearly says to keep on asking the user to enter a hyphenated word unless the value is valid, i.e. unless the value contains a hyphen (-) character in it.

This reminds us of a loop, specifically a while loop, whose condition is set to check if the input value doesn't contain a hyphen (-) character.

First, let's set up the input prompt:

<?php

echo 'Enter a hyphenated word: ';
$word = rtrim(fgets(STDIN));

And now, let's set up the while loop:

<?php

echo 'Enter a hyphenated word: ';
$word = rtrim(fgets(STDIN));

while (!str_contains($word, '-')) {
   // The error message.
   echo 'Invalid input', "\n\n";

   echo 'Enter a hyphenated word: ';
   $word = rtrim(fgets(STDIN));
}

As you can see here, the input prompt code has been repeated inside the body of the while loop.

For now, this isn't a problem at all since the code is short and simple, and so there's not going to be a big issue even if we repeat it. Had it been a complex piece of code, we would've considered another better way.

Anyways, moving on, once the loop exits, we know that the value of $word is valid. Likewise, we could proceed with extracting out the individual words from $word after the while loop.

For the output, $word is split into a list at each hyphen (-) character using explode(), and then each element of this list is printed out using a for loop, after printing a blank line:

<?php

echo 'Enter a hyphenated word: ';
$word = rtrim(fgets(STDIN));

while (!str_contains($word, '-')) {
   echo 'Invalid input', "\n\n";
   echo 'Enter a hyphenated word: ';
   $word = rtrim(fgets(STDIN));
}

// At this point, $word is valid. Likewise, we can proceed with the output

echo "\n";
$words = explode('-', $word);
for ($i = 0; $i < count($words); $i++) {
   echo $words[$i], "\n";
}

This completes our exercise!