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

#include <iostream>
using namespace std;

void trimFunction( string & fileName, string & editFileName, string & properFileName);


void main()
{

	string fileName, editFileName, properFileName;
	
	cout << "Enter fileName"<<endl;
	getline(cin, fileName);
	
    trimFunction( fileName, editFileName, properFileName);
    

    
 }		 		
  
void trimFunction( string& fileName, string& editFileName, string& properFileName){

  	int i = fileName.length()-1;
  	int endStr =i;
  	int numChars=0;
  	char ch;
  	ch = fileName.at(i);		//gets end of fileName char
  	
  	cout <<"i "<<i<<endl;
  	
	while(ch !='*' && i >=0)
  	{	
  		
  		  i--;
  	      cout <<"i loop "<<i<<endl;
  		  ch = fileName.at(i);
  		  cout<< "Ch = " << ch <<endl;
  	}
  	
  	numChars = endStr -i;
  	editFileName = fileName.substr(i+1, numChars);
  	
  	cout << "File Name before editing "<< fileName <<endl;
    cout << "File Name after editing  "<< editFileName << endl;
     
   int startAddress =0;
   int j = fileName.length();
   int numChars2 = j - startAddress ;
   char ch2;
   ch2 = fileName.at(j);
   
   while( ch2 !='*' && j < fileName.length() )
  {
			j++;
			cout <<"i loop "<<j<<endl;
			ch2 = fileName.at(j);
	        cout<< "Ch = " << ch <<endl;

			
   }
   
   properFileName = fileName.substr(j-1, numChars2);
  	
   cout << "Edit File Name after editing  "<< properFileName << endl;
   		
  }
Member Avatar for iamthwee

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

int j = fileName.length();
   int endStr2 = j;
   int numChars2 = 0 ;
   char ch2;
   ch2 = fileName.at(j);
   
   while( ch2 !='*' && j >= 0 )
  {
			j++;
			cout <<"i loop "<<j<<endl;
			ch2 = fileName.at(j);
	        cout<< "Ch = " << ch <<endl;

			
   }
   
   properFileName = fileName.substr(1-j, numChars2);
  	
   cout << "Edit File Name after editing  "<< properFileName << endl;

Tryed this with the following code but i'm still doing something wrong!

Member Avatar for iamthwee
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <iterator>

using namespace std;


void trim(string str)
{
  string::size_type pos = str.find_last_not_of('*');
  if(pos != string::npos) {
    str.erase(pos + 1);
    pos = str.find_first_not_of('*');
    if(pos != string::npos) str.erase(0, pos);
  }
  else str.erase(str.begin(), str.end());
  
  cout << str;
}

int main()
{
    trim("*********************jfdkslfjdsfj**lds************");
    cin.get();
}

output:

jfdkslfjdsfj**lds
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.