Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hey everyone, I\'m having problem in my project. what I\'m actually trying to do

ID: 3840359 • Letter: H

Question

Hey everyone, I'm having problem in my project. what I'm actually trying to do for example, I have : string path = "akjs/flkdldksd:ksdfkjsdn:ksdjfskjsdhf/jdsfjkds:kjsdf/hsdf/ghvsd/hsdf:sdhd/hjsd:"

I need to use substr and string function find(), find_first_of() on path to print each entry before and after ":" in sperated line with size of the entry on the same line.

So i need a loop to print the rest of the entries, after that I need the total of the entries and the total size of the entries.

I have used getnev(); to get the path. please I need help.

Thanks in advance.

Explanation / Answer

#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;

int main(){
   string path = getenv("PATH");
   //delete following line
   path = "akjs/flkdldksd:ksdfkjsdn:ksdjfskjsdhf/jdsfjkds:kjsdf/hsdf/ghvsd/hsdf:sdhd/hjsd";
   while( true ){
       int at = path.find(":");
       if( at == string::npos ){
           cout << path << endl;
           break;
       }
       cout << path.substr(0, at) << endl;
       path = path.substr(at+ 1);
   }
   return 0;
}