Getting Rid of Extra Blank at the End

Best way to delete it is by not printing it in the first place.

Test to make sure you have at least one printable input and print it. Then for all remaining inputs print out the delimiter followed by the input.

#include<iostream>

int main(int argc, char *argv[]) {
    if (argc > 1)
    {  // Ensure that where is at least one argument to print
        std::cout << argv[argc - 1]; // print last argument without adornment
        for(int num = argc - 2; num > 0; num--)
        { // Print a space and all remaining arguments. 
          // For visibility, I've replaced the space with an underscore 
            std::cout << "_" << argv[num] ;
        }
    }
/* unsure what this loop is supposed to do. Doesn't do anything in it's current state, 
   so I've commented it out.
    for(int num = argc; num < 2; num--) 
    {
        std::cout << argv[num - 1];
    }
*/
    return 0;
}

Input:

first second third

Output:

third_second_first

Leave a Comment