0

I was trying to solve this problem but I think I am not doing the string handling part right. The problem is given a string (let's say "abc") write out all upper and lower case combinations of this string.

My approach was to modify binary counter method.

So here's my implementation:

#include <iostream> #include <cmath> #define LOWER_CASE_DIFF 'a'-'A' using namespace std; void changeSeq(string &in, int amount) { int i = 0; while (i < amount && (int)in[i] < 'a') { in[i] += LOWER_CASE_DIFF; i++; } if (i < amount) { in[i] -= LOWER_CASE_DIFF; } cout << in << endl; } int main() { string input = "abc"; int diff = 'a' - 'A'; //a is always bigger than A in ASCII int comb = (int)pow(2,(float)input.length()); for (int i = 1; i <= comb; i++) { changeSeq(input, i); } return 0; } 

I am getting this runtime error:

/usr/local/lib/gcc/i686-pc-linux-gnu/4.1.2/../../../../include/c++/4.1.2/bits/basic_string.h:707: typename _Alloc::rebind<_CharT>::other::reference std::basic_string<_CharT, _Traits, _Alloc>::operator[](typename _Alloc::rebind<_CharT>::other::size_type) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]: Assertion '__pos < size()' failed. Disallowed system call: SYS_kill 

So how can I change one character at a time? Is strings behaviour in C++ similar to const char* str = "abc" in C, where array of characters are stored in constants?

10
  • 1
    You can change the contents of the string, but I think your problem is with indexing - i.e. accessing input[6] is not valid if the string is less than 6 characters long. Did you at least debug to see where it breaks? Commented Nov 20, 2013 at 10:58
  • 1
    (int)pow(2,(float)input.length()) is a very bad idea. Floating-point numbers are not exact, so it is possible that pow(2, 3) == 7.99999 and thus (int)pow(2, 3) == 7... Commented Nov 20, 2013 at 10:58
  • Interesting, it works for me: ideone.com/VRLPNI Commented Nov 20, 2013 at 10:59
  • Furthermore, it seems to me that you are looking for permutations of the string. There are n! of those, and not 2 ^ n. Commented Nov 20, 2013 at 10:59
  • 2
    @H2CO3 judging from his changeSeq it seems as if he just wants ABC AbC not BCA BaC .. so 2^n seems right Commented Nov 20, 2013 at 11:07

3 Answers 3

1

You could do something like this

 string s = "ABC"; int comb = 1 << s.length(); for (int i = 0; i < comb; ++i) // 0000 0001 0010 ... 1000 { for ( size_t j = 0; j < s.length(); ++j ) { if ( i & (1 << j) ) { s[j] = tolower(s[j]); } else { s[j] = toupper(s[j]); } } cout << s << endl; } 

probably would be better to include a

bool testbit(int value, int bit) { return value & (1 << bit); } 

to make the code more readable.

Sign up to request clarification or add additional context in comments.

Comments

0

You need to include the string header

#include <string> 

Other then that, it seems to be working fine in VS2013

Comments

0

I think you are over-complicating thing when trying to compute the number of possible outputs; maybe this'll help:

for(i=0;i<input.length();i++) { for(j=0;j<input.length();j++) { printString(input); changeCase(input[j]); } printString(input); changeCase(input[i]); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.