943,907 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1375
  • C++ RSS
Nov 5th, 2008
0

Trim function

Expand Post »
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

C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 11
Solved Threads: 0
Light Poster
Seamus McCarthy is offline Offline
34 posts
since Apr 2008
Nov 5th, 2008
0

Re: Trim function

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
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Nov 5th, 2008
0

Re: Trim function

C++ Syntax (Toggle Plain Text)
  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!
Reputation Points: 11
Solved Threads: 0
Light Poster
Seamus McCarthy is offline Offline
34 posts
since Apr 2008
Nov 5th, 2008
0

Re: Trim function

C++ Syntax (Toggle Plain Text)
  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:

C++ Syntax (Toggle Plain Text)
  1. jfdkslfjdsfj**lds
Last edited by iamthwee; Nov 5th, 2008 at 11:54 am.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: sorting a text file
Next Thread in C++ Forum Timeline: VERY BASIC C++ >> reading from a text file





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC