I have a binary string like "01001111111". I want to know that if that string contains "0" or not. How to do that ? And if I want to convert it into integer, how to get that ?
Asked
Active
Viewed 889 times
-1

HostileFork says dont trust SE
- 32,904
- 11
- 98
- 167

user1838343
- 451
- 2
- 8
- 16
-
I want to know is there any library function like Java (string.contains or parseInt). – user1838343 Nov 20 '12 at 12:31
-
Which are your requirements? Why a simple loop is not a sensible solution for yout problem? – akappa Nov 20 '12 at 12:32
-
@user1838343: http://www.google.com/search?q=c%2B%2B+string+contains – Karoly Horvath Nov 20 '12 at 12:32
-
3@user1838343 As you are new, I'll explain why you're getting downvotes. They come from the idea that you have not demonstrated an effort to try using Google or other sources to answer this common question, nor have you given an example of something you actually tried that didn't work. While beginner questions are perfectly acceptable, it's better to show you've gotten to the point of some non-working code that a search engine won't automatically help with. – HostileFork says dont trust SE Nov 20 '12 at 12:35
1 Answers
3
Since this seems to be a homework, so giving complete solution would not be a nice idea.
However, I would give you this hint:
std::bitset
std::string
and related functions from<algorithm>
header.
These two together should solve your problem if you learn to use them cleverly.

Nawaz
- 353,942
- 115
- 666
- 851
-
1Note that there is precisely one integer (signed or unsigned) that contains *no* `0` in its binary representation: `-1`. – bitmask Nov 20 '12 at 12:32
-
-
2@user1838343: It's expected of you to contribute to the solution. `std::bitset` is already a *very* good hint. Look up how it works, and the answer will be self-evident. – bitmask Nov 20 '12 at 12:34
-
1@bitmask You might think, but some teachers are pretty bad...some students don't pay attention...and sometimes both are true. So the groundwork for library documentation making any sense may not be laid down. Not having an immediate intuitive leap to a solution is not the problem, it's the lack of demonstration of effort and hitting a clearly defined wall. As the refrain goes: *"what have you tried?"* – HostileFork says dont trust SE Nov 20 '12 at 12:53
-
1@HostileFork: I don't get what you are saying *at all*. Why do you need groundwork to read library documentation? It's there. Once you know a couple sources where to find handy documentation (like provided in this answer, that I totally forgot to upvote, sorry Nawaz) nobody has to teach you anything. As a matter of fact I haven't received one minute of C++ tutoring, and frankly I don't see how that would have been helpful. – bitmask Nov 20 '12 at 13:01
-
@bitmask Easy to assume everything is clear when you see past inherent contradictions. I taught someone version control recently, who came back with a list of very valid questions like "what's the difference between a version and a revision and a delta and a diff and a commit?" If you don't see things from a fresh point of view, the interchangeable nature of version and revision might not bother you...but *"revision 1"* for an initial copy confuses because what (precisely) does a *first* version "revise"? Have empathy for those who don't understand--save contempt for those who don't *try*. – HostileFork says dont trust SE Nov 20 '12 at 13:08
-
1Yeah, introducing a screwed up svn-user to git for example is hell. However, I didn't show (or didn't intend to show) contempt for anybody. I just pointed out that with the documentation of library functions you will need handed to you on a silver platter is really all you can ask for. – bitmask Nov 20 '12 at 13:18