strlen() is slow. It has to start at the beginning of the string and see if each succeeding character is a NULL or not. I only call strlen() once, though.
isspace() is very fast, and besides you would need to call it too.
strlen() is slow. It has to start at the beginning of the string and see if each succeeding character is a NULL or not. I only call strlen() once, though.
isspace() is very fast, and besides you would need to call it too.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Hi sly im not sure what you mean in your example if you get time could you put up example code (sorry im a bit slow at things like this)
This is what Sly means:
Code:size_t x = 0; char other[somesize]' while(isspace(s[x++])); strcpy(other, &s[x]); strcpy(s, other);
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
why use library functions?
Code:char * trim(char * dst, const char * src) { char * result = dst; while(*src == ' ') { ++src; } while(*src != ' ' && *src != 0) { *dst++ = *src++; } *dst = 0; return result; } char * trim(char * str) { return trim(str, str); }
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
*claps* I was looking for something similar to that without library functions. I couldn't think of it, and then dinner was ready. Nice one.
Sent from my iPadŽ
...because you have to consider your audience?why use library functions?
I'm not sure how that is supposed to work. Here is an example sentence that was read into a char array using getline():A nice simple solution is reading in the beginning with a regular extraction eliminating the beginning whitespace, reading in the rest with a getline using a space delimeter
and the op would like to remove the leading and trailing spaces.Code:" Hi my name is Kim. "
You are advising to read in the first word using operator>>() to get "Hi". Check. That removes the leading whitespace. Then, you are suggesting using getline() with a space as the delimiter to read in the rest of the sentence? Won't that only get you " my"?
Last edited by 7stud; 02-18-2006 at 07:53 PM.
Easiest solution for removing the initial whitespaces would be to declare another char pointer and then iterate through the string till the first non-whitespace character is found and let the the char pointer store the adress for that position. This way the whitespaces aren't actually removed and you won't need to move around the whole array.
[edit] Oh, and this would mean that you would have to use this new char pointer for whatever task you needed to do instead of your array called "s".
Last edited by OnionKnight; 02-18-2006 at 07:51 PM.
...and then what?let the the char pointer store the adress for that position.
I don't know what you mean by "then what" but here's an example of the array "s" after the trailing whitespaces are gone and the position for the first non-whitespace character is found and also pretending that the first character of "s" had the adress of 340 and that it'sthe array has become really small.
This would make the new char pointer contain the adress s+3, or 340+3 or 343.Code:340 343 | | v v [' '][' '][' ']['s']['t']['h']['\0'][' '][' ']
I don't see any problem with this solution.
in that case you'd need to return two pointers: one to the first non-whitespace and another to last non-whitespace character (or even a pointer/length pair).
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
Relying on the zero termination for the last non-whitespace character shouldn't cause any trouble.
Yeah for boredom:
Code:#include <iostream> #include <string> using namespace std; string trim(const string&); int main() { string test = " Hello I'm a test string! "; string trimed = trim(test); cout<<"Test: \""<<test<<"\""<<endl; cout<<"trimed: \""<<trimed<<"\""<<endl; cin.ignore(); } string trim(const string& st) { int first = st.find_first_not_of(" "); int last = st.find_last_not_of(" "); first = first == string::npos ? 0 : first; return st.substr( first, last-first ); }
Yes, you're correct. I hadn't followed up on it because I noticed its problems. Infact, the truth is, if you used a space delimeter, it wouldn't even get to the my and just read nothing.Originally Posted by 7stud
Sent from my iPadŽ