1
0

basename.cpp 701 B

1234567891011121314151617181920212223242526
  1. #include <stdlib.h>
  2. #include <vector>
  3. #include <iostream>
  4. void echo(std::string a);
  5. std::string getBaseName(std::string s);
  6. int main(int argc, char *argv[]){
  7. // if user uses program incorrectly, print error message & exit.
  8. if(argc < 2) {
  9. printf("Usage: %s {filename.ext}\n", argv[0]);
  10. return 1;
  11. }
  12. std::string userinput = argv[1];
  13. echo(getBaseName(userinput));
  14. }
  15. void echo(std::string a){
  16. printf("%s\n",a.c_str());
  17. }
  18. std::string getBaseName(std::string s) {
  19. // getBaseName creates a substring up until the period character
  20. for(int j=s.size();j>0;j--){
  21. if(s[j]== '.'){
  22. return s.substr(0,j);
  23. }
  24. }
  25. return s.substr(0,0);
  26. }