Showing Subcategory’s Name and Link Instead of Parent Category’s

Little change:

<?php $category_separator = "";
foreach((get_the_category()) as $childcat) {
     $parentcat = $childcat->term_id;
     if( $parentcat != 0 ) echo '<span class="' . get_cat_name($parentcat) .'"><a href="' . get_category_link($parentcat) . '">' .get_cat_name($parentcat) .'</a></span>';
     else echo '' . $category_separator . '<span class="'. $childcat->cat_name .'"><a href="' . get_category_link($childcat) .'">' . $childcat->cat_name . '</a></span>';
    $category_separator = "https://wordpress.stackexchange.com/";
}
?>

Actually you were getting the parent category id

 $parentcat = $childcat->category_parent;

Instead of that you need to get actual sub category id

$parentcat = $childcat->term_id;