Exercise: Abscissa and Ordinate

Exercise 12 Easy

Prerequisites for the exercise

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

Objective

Extract out the abscissa and ordinate from an input co-ordinate point.

Description

A co-ordinate is a point on a Cartesian plane, having a value for the x-axis and a value for the y-axis.

It's usually denoted as follows:

(x, y)

The first number inside the parentheses is the value for x-axis, formally known as the abscissa. The second number is the value for the y-axis, formally known as the ordinate.

In this exercise, given a co-ordinate, you ought to extract out the abscissa and ordinate from it, and then print them, each on a new line.

Ask the user to input a co-ordinate with the following prompt message:

Enter a co-ordinate:

The input value should be of the following form: (x,y), where x is the abscissa and y is the ordinate.

You shall assume that there is no space in the input value between x, , and y. So, for example, you don't need to worry about an input like (3, 1) — it'll always be like (3,1).

Once the co-ordinate is given, extract out the abscissa and ordinate from it and output them, each on a new line, after leaving a blank line to start with.

A couple of examples follow:

Enter a co-ordinate: (3,1)

Abscissa: 3
Ordinate: 1
Enter a co-ordinate: (-3.5,10.4)

Abscissa: -3.5
Ordinate: 10.4

Hints

Hint 1

Find the index of the comma (,) in the input value using the strpos() string function.

Hint 2

Slice the input value in two portions using the substr() function.

View Solution

New file

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

Solution

To solve this exercise, we just need to know two things: how to find a given substring inside a string and how to slice a string in PHP. If we know both of these, we're good to go!

Step one is to set up an input prompt and receive the co-ordinate from the user, as stated in the exercise's description. We'll call this input value $point.

Let's get done with this first:

<?php

echo 'Enter the co-ordinate: ';
$point = rtrim(fgets(STDIN));

Step two is to find the index of the comma (,) in $point. This is required so that we could slice $point in two halves at this position and then extract out the abscissa and ordinate from each respective half.

We'll use the strpos() string function to find the index of the comma in $point.

<?php

echo 'Enter the co-ordinate: ';
$point = rtrim(fgets(STDIN));

$index = strpos($point, ',');

Once the index of the comma is found, the next step is compute the abscissa.

The abscissa starts right after the first parenthesis (() in the string $point and ends just before the comma (,). So to extract it, we ought to slice $point from index 1 upto the point where the comma exists (but obviously excluding the comma).

For the slicing, we'll use the string function substr(), keeping in mind that the third argument of substr() takes the length of the slice, NOT its ending position, and that's why we'll have to perform some quick arithmetic to get the correct value for this argument.

Here's the code to do so:

<?php

echo 'Enter the co-ordinate: ';
$point = rtrim(fgets(STDIN));

$index = strpos($point, ',');
$abscissa = substr($point, 1, $index - 1);

Next up, we move to extract the ordinate.

The ordinate begins right after the comma (,) and ends just before the ending parenthesis ()). Likewise, it could be extracted as follows:

<?php

echo 'Enter the co-ordinate: ';
$point = rtrim(fgets(STDIN));

$index = strpos($point, ',');
$abscissa = substr($point, 1, $index - 1);
$ordinate = substr($point, $index + 1, -1);

With the abscissa and ordinate in hand, we make the desired output keeping in mind that we have to leave a blank line before doing so.

Altogether, we get the following code:

<?php

echo 'Enter the co-ordinate: ';
$point = rtrim(fgets(STDIN));

$index = strpos($point, ',');
$abscissa = substr($point, 1, $index - 1);
$ordinate = substr($point, $index + 1, -1);

echo "\n";
echo 'Abscissa: ', $abscissa, "\n";
echo 'Ordinate: ', $ordinate;

And this completes the exercise.