Trim function

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2008
Posts: 34
Reputation: Seamus McCarthy is an unknown quantity at this point 
Solved Threads: 0
Seamus McCarthy Seamus McCarthy is offline Offline
Light Poster

Trim function

 
0
  #1
Nov 5th, 2008
Hey i'm trying to use this trim function i wrote but its going wrong for me. I can trim from the front but not behind, e.g. if i enter **bob it works but if i enter **bob** it just prints nothing. please help

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void trimFunction( string & fileName, string & editFileName, string & properFileName);
  5.  
  6.  
  7. void main()
  8. {
  9.  
  10. string fileName, editFileName, properFileName;
  11.  
  12. cout << "Enter fileName"<<endl;
  13. getline(cin, fileName);
  14.  
  15. trimFunction( fileName, editFileName, properFileName);
  16.  
  17.  
  18.  
  19. }
  20.  
  21. void trimFunction( string& fileName, string& editFileName, string& properFileName){
  22.  
  23. int i = fileName.length()-1;
  24. int endStr =i;
  25. int numChars=0;
  26. char ch;
  27. ch = fileName.at(i); //gets end of fileName char
  28.  
  29. cout <<"i "<<i<<endl;
  30.  
  31. while(ch !='*' && i >=0)
  32. {
  33.  
  34. i--;
  35. cout <<"i loop "<<i<<endl;
  36. ch = fileName.at(i);
  37. cout<< "Ch = " << ch <<endl;
  38. }
  39.  
  40. numChars = endStr -i;
  41. editFileName = fileName.substr(i+1, numChars);
  42.  
  43. cout << "File Name before editing "<< fileName <<endl;
  44. cout << "File Name after editing "<< editFileName << endl;
  45.  
  46. int startAddress =0;
  47. int j = fileName.length();
  48. int numChars2 = j - startAddress ;
  49. char ch2;
  50. ch2 = fileName.at(j);
  51.  
  52. while( ch2 !='*' && j < fileName.length() )
  53. {
  54. j++;
  55. cout <<"i loop "<<j<<endl;
  56. ch2 = fileName.at(j);
  57. cout<< "Ch = " << ch <<endl;
  58.  
  59.  
  60. }
  61.  
  62. properFileName = fileName.substr(j-1, numChars2);
  63.  
  64. cout << "Edit File Name after editing "<< properFileName << endl;
  65.  
  66. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Trim function

 
0
  #2
Nov 5th, 2008
Should be simple,

Start from the end and keep going back one checking if the previous one was a '*', when this fails note the integer position and substring it.

simple
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 34
Reputation: Seamus McCarthy is an unknown quantity at this point 
Solved Threads: 0
Seamus McCarthy Seamus McCarthy is offline Offline
Light Poster

Re: Trim function

 
0
  #3
Nov 5th, 2008
  1. int j = fileName.length();
  2. int endStr2 = j;
  3. int numChars2 = 0 ;
  4. char ch2;
  5. ch2 = fileName.at(j);
  6.  
  7. while( ch2 !='*' && j >= 0 )
  8. {
  9. j++;
  10. cout <<"i loop "<<j<<endl;
  11. ch2 = fileName.at(j);
  12. cout<< "Ch = " << ch <<endl;
  13.  
  14.  
  15. }
  16.  
  17. properFileName = fileName.substr(1-j, numChars2);
  18.  
  19. cout << "Edit File Name after editing "<< properFileName << endl;

Tryed this with the following code but i'm still doing something wrong!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Trim function

 
0
  #4
Nov 5th, 2008
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <vector>
  5. #include <iterator>
  6.  
  7. using namespace std;
  8.  
  9.  
  10. void trim(string str)
  11. {
  12. string::size_type pos = str.find_last_not_of('*');
  13. if(pos != string::npos) {
  14. str.erase(pos + 1);
  15. pos = str.find_first_not_of('*');
  16. if(pos != string::npos) str.erase(0, pos);
  17. }
  18. else str.erase(str.begin(), str.end());
  19.  
  20. cout << str;
  21. }
  22.  
  23. int main()
  24. {
  25. trim("*********************jfdkslfjdsfj**lds************");
  26. cin.get();
  27. }

output:

  1. jfdkslfjdsfj**lds
Last edited by iamthwee; Nov 5th, 2008 at 11:54 am.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC