Add codc the prints out all of the command line arguments from 0 to argc. ii.Add
ID: 3685924 • Letter: A
Question
Add codc the prints out all of the command line arguments from 0 to argc. ii.Add code that finds the first string in the argument list that has an extension txt". iii.Create and print a string with the same base filename (characters before the txt") but with the extension ". out". iiii. If no arguments exist with the txt" extension, the print the following error message: "Invalid input filename." Run your codc with the following test commands Test case xecutlen command 1.a.Out input,txt 1 2 3 4 5 2.a.Out 1 2 input.txt 3 4 5 3.a.out 1 2 3 4 5 6 Here is some sample code for this part. It is up to you to make it work as defined.//note arqct and ragvt are not final names of the//parbalms of the asin fundtion this//tais is a sample. arge and argr should be the finalExplanation / Answer
// Source Code to Copy :-
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
// g++ sample.cpp -o example
// ./example Hi Hello
int main ( int argc, char *argv[] )
{
cout<<"Arguments : " << "argc : Count of Arguments including File Name :"<< argc ;
for(int i=0 ;i < argc ;i++){
cout << " arg[" << i <<"] : " << argv[i] <<" ";
}
/*
The position of the first character of the first match.
If no matches were found, the function returns string::npos.
size_t is an unsigned integral type (the same as member type string::size_type).
*/
for( int i=0 ; i < argc ; i++){
string str = argv[i];
string str2 = ".txt";
std::size_t found = str.find(str2);
if (found!=std::string::npos){
std::cout << "'.txt' also found at: " << found <<" location of argument number" << i << " ";
// For Replacing
if(str.length() != 4) { // Checks if .txt is valid and present as extension
str.replace(str.find(str2),str2.length(),".out"); // replaces extension .
std::cout << " replaced with " << str << ' ';
} else{
cout << " Invalid File ";
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.