| | |
Trim function
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2008
Posts: 34
Reputation:
Solved Threads: 0
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)
#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; }
•
•
Join Date: Apr 2008
Posts: 34
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
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!
C++ Syntax (Toggle Plain Text)
#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:
C++ Syntax (Toggle Plain Text)
jfdkslfjdsfj**lds
Last edited by iamthwee; Nov 5th, 2008 at 11:54 am.
*Voted best profile in the world*
![]() |
Similar Threads
- trim function not working (PHP)
- Searching Function at listview by using VB.Net (VB.NET)
- trim function (ASP)
- Extracting a field in a txt file (C)
- how to store item and invoke from it (Java)
- trim trailing spaces (C++)
- "hey STUPID finish the form" function? (PHP)
- 35 processes, need to trim the fat (Viruses, Spyware and other Nasties)
Other Threads in the C++ Forum
- Previous Thread: sorting a text file
- Next Thread: VERY BASIC C++ >> reading from a text file
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamic dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library linker list loop looping loops map math matrix memory microsoft newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings struct temperature template templates test text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






