Command Line Arguments in C++

This C++ program illustrates the use of command line arguments. The parameter argc passed to the main of the C++ program contains the number of arguments passed to the main function and the argv parameter contains the pointers to the character strings passed as the command-line arguments. Based on the value of these arguments, the case of the string entered can be changed accordingly.

Here is the source code of the C++ program illustrates the use of command line arguments. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C++ Program to Illustrate Command Line Arguments
  3.  */
  4.  
  5. #include <iostream>
  6. #include <cctype>
  7. #include <string>
  8.  
  9. int main(int argc, char* argv[])
  10. {
  11.     if (argc < 2)
  12.     {
  13.         std::cout << "Command-line Argument missing " << std::endl;
  14.         return 0;
  15.     }
  16.     else if (argc == 2)
  17.     {
  18.         if (strcmp(argv[1], "toupper") == 0)
  19.         {
  20.             std::string str;
  21.  
  22.             std::cout << "Enter the string ";
  23.             std::cin >> str;
  24.             for (int i= 0; i < str.length(); i++)
  25.             {
  26.                 str[i] = toupper(str[i]);
  27.             }
  28.             std::cout << "The string is now " << str
  29.                       << std::endl;
  30.         }
  31.         else if (strcmp(argv[1], "tolower") == 0)
  32.         {
  33.             std::string str;
  34.  
  35.             std::cout << "Enter the string ";
  36.             std::cin >> str;
  37.             for (int i= 0; i < str.length(); i++)
  38.                 str[i] = tolower(str[i]);
  39.             std::cout << "The string is now " << str
  40.                       << std::endl;
  41.         }
  42.         else
  43.             std::cout << "Unmatched second command-line argument"
  44.                       << std::endl;   
  45.     }
  46.     return 0;       
  47. }

$ a.out toupper
Enter the string programming
The string is now PROGRAMMING
$ ./a.out tolower
Enter the string PROGRAMMING
The string is now programming
$ ./a.out
Command-line Argument missing

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.