detecting if a string has spaces or is empty

Started by
13 comments, last by ELS1 22 years ago
I''m assuming you wanted to detect if it was blank, or was FILLED with spaces, not just contained a single space?


  bool IsNull(char *String){ if (!String) //Is our pointer null?  return 1; //if so, then no string! while (*String && *String=='' '') //not Terminated && a Space?  ++String; //Increment our pointer to next value if (!*String) //Did our loop go to our terminating char?  return 1; //Nothing found! return 0; //It contains something other than spaces!}  


Billy - BillyB@mrsnj.com

ps. If you need anything else, you can directly contact me at my email address provided.
Advertisement
SabreMan:
"First off - const correctness."

Const correctness doesn''t allow you to optomize at all.. actually.. the opposite. Check out my function and try to replace it with a "const correct" function and see how much optomization it gives you.

By saying that String is to be constant, you are forcing yourself to create at least 1 variable to point or to count with. You could make this a static variable so it''s only created once.. but then you just wasted 4 bytes of memory and an assignment (the assignment per call). Const does help in some readability I guess, but don''t say that it can help optomize, because... well.. there is NOTHING you can''t do with a normal pointer that you can do with a const pointer, inversely, there are things you can do with a non const pointer that you can''t do with a const one.

Secondly: I kind of agree with this one, but could disagree on this particular case (most cases I would have agreed though, as working with the members is what the function "should" be doing). But, what is he losing by passing it as an argument instead of using it directly (again, refer to my function). If he used it directly, he would have to make another pointer to the original or use an index variable... either way, it''s not any more efficient. Also, he can now call this function from outside of the class and use it if necessary... but, it probably wouldn''t be a bad idea for him to not pass anything to this one, and just use his class member variables.

3rd: Agreed.

Billy - BillyB@mrsnj.com
quote:Original post by Anonymous Poster
SabreMan:
"First off - const correctness."

Const correctness doesn''t allow you to optomize at all..

Yes it does, albeit only rarely. "ROMable" PODs can have better code generated.

quote:actually.. the opposite. Check out my function and try to replace it with a "const correct" function and see how much optomization it gives you.

By saying that String is to be constant, you are forcing yourself to create at least 1 variable to point or to count with.

Not at all. For a NTCS, at the very least you should pass a const char*. This copies the pointer, but ensures that it''s a pointer to const char. You don''t have to create another variable to point, you use the one passed by value, but you ensure you can''t accidentally modify the characters of the string.

quote:You could make this a static variable so it''s only created once.. but then you just wasted 4 bytes of memory and an assignment (the assignment per call). Const does help in some readability I guess, but don''t say that it can help optomize, because... well.. there is NOTHING you can''t do with a normal pointer that you can do with a const pointer,

On the contrary. The pointer to a const variable can point to ROM or otherwise read-only memory.

Why is this of consequence?

If you declare the function as taking const char*, you can pass any string -- including ones from, for instance, std::string::c_str(), and strings that are pooled in read-only memory (as most optimizing compilers will do) -- strings where you''re not allowed to modify the data.

If you declare the function as taking char*, you can''t -- not without casting away the constness, at least.

quote:inversely, there are things you can do with a non const pointer that you can''t do with a const one.

Which aren''t pertinent here. The pointer is being passed by value, we can modify that all we like. It''s modifying the pointed-at things which is dangerous, which is why we want to prevent it with a const-correct function.

char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;}  /*** drpizza@battleaxe.net ***/
As Oluseyi said there are standard c library functions for this sort of thing. However instead of using strtok I suggest strchr (to find if a string has a space) or strspn (to find if a string is all spaces).

Another thing to keep in mind if you''re trying to validate input is that you probably care about more just spaces. You usually don''t want your string to have control characters or whitespace in general (i.e. tabs, newlines, etc). To do this, take any of the for/while loop solutions above and replace the (*p=='' '') test with (isspace(*p) || !isprint(*p)).

Also don''t forget about leading/trailing spaces in otherwise valid strings. i.e. "foo bar" might be ok but you might not want "foo bar ".

-Mike

Billy, I don''t understand why you''re wailing about const correctness. You can make your function const correct simply by replacing "bool IsNull(char *String)" with "bool IsNull(const char *String)" and VC will generate the *exact* same code. for both versions.
-Mike
quote:Original post by Anon Mike
Billy, I don''t understand why you''re wailing about const correctness. You can make your function const correct simply by replacing "bool IsNull(char *String)" with "bool IsNull(const char *String)" and VC will generate the *exact* same code. for both versions.

That''s not the point really - const correctness helps catch errors. So that if he accidentally typed *String = '' '' instead of *String == '' '', it would catch it at compile time.



[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost ]

This topic is closed to new replies.

Advertisement