String itteration

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yeshello54
    New Member
    • Mar 2009
    • 54

    String itteration

    Hello all..so i have this working bit of code..i made it to big and complex i think..and i was wondering if anyone had any insight on how i could compact it a little better..

    let me explain . first i take the filename from argv[1].then i replace the .asm with a .lis. this then makes the filename source.lis.asm. so I erase the .asm extension to finally get source.lis. but my code seems a little ridiculous.haha . but any help would be cool. thanks.

    argv[1]="source.asm "

    string::iterato r it;
    string str1=".lis";
    string filename;
    filename = *argv[1] ;
    for (int r=0; r<filename.leng th(); r++)
    {
    filename= argv[1] ; //at this point i have source.asm
    }

    it=filename.end ()-4;
    filename.replac e(it,filename.e nd()-4,str1); //at this point i have source.lis.asm
    filename.erase( filename.end()-4); //now filename has source.lisasm
    filename.erase( filename.end()-3); //now filename has source.lissm
    filename.erase( filename.end()-2); //now filename has source.lism
    filename.erase( filename.end()-1); //now filename has source.lis
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Can you not just use the string member functions string::find and string::replace ?

    For some classes like string that you will be using a lot it is worth reading the documentation so you become familiar with what methods are available.

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      This
      Code:
      argv[1]="source.asm"
      
      string::iterator it;
      string str1=".lis";
      string filename;
      filename = *argv[1] ;
         for (int r=0; r<filename.length(); r++)
      {
      filename= argv[1] ;          //at this point i have source.asm
      }
      is equivalent to this

      string filename= argv[1];

      This

      Code:
      it=filename.end()-4;
      filename.replace(it,filename.end()-4,str1);    //at this point i have source.lis.asm
      filename.erase(filename.end()-4);        //now filename has source.lisasm
      filename.erase(filename.end()-3);        //now filename has source.lissm
      filename.erase(filename.end()-2);       //now filename has source.lism
      filename.erase(filename.end()-1);       //now filename has source.lis[/QUOTE]
      is equivalent to this

      string str1=".lis";
      filename.replac e(filename.end( )-4,filename.end( ),str1);

      Comment

      • yeshello54
        New Member
        • Mar 2009
        • 54

        #4
        thanks for the help. Ya the only reason i did the
        string filename =*argv[1]
        then the for loop was because later i have argv[1] passed into a function and it would give me some error with memory allocation. But I will continue on to try to shorten the code with your help.Thanks.

        Comment

        Working...