Today my teacher was teaching us how we could use pointers in C to simulate some common functions of this programming language, one of his examples was the rev_string
function (shown below) which was created to simulate original strrev()
from the <string.h>
header.
void rev_string(char *s)
{
char *t;
int counter = 0, middle, temp, i, j, last, begin;
for (t = s; *t != '\0'; t++)
counter++;
middle = (counter % 2 == 0) ? (counter / 2) : ((counter - 1) / 2);
j = counter - 1;
for (i = 0; i < middle; i++)
{
last = s[j];
begin = s[i];
temp = last;
s[j] = begin;
s[i] = temp;
j--;
}
}
After looking at the code above several times, I could not figured out the the use of ?
and :
declared inside the middle
variable. Could anyone explain me why are those symbols necessary in the code above?