Reputation: 1416
I suppose I just never learned this. I have never done this before. I have seen the use of strcat(S1, S2)
, but that isn't applicable here, is it?
Can I do something like
string all_possible_strings[10];
char jumbled_chars[] = "ABCDEFG";
all_possible_strings[1] = jumbled_chars[0] << jumbled_chars[1]
<< jumbled_chars[2] << jumbled_chars[3]
<< jumbled_chars[4];
What I'm trying to do is make a program that can unscramble a word into all of its possible permutations.
Upvotes: 1
Views: 155
Reputation: 12397
Use the append
function or the operator+=
overload of std::string
. You should read up on the STL documentation.
If jumbled_chars
is already in the order you want it, then you could just construct the string like
all_possible_strings[counter] = std::string(jumbled_chars, 5);
Update:
Ok, here's a few suggestions. Instead of storing your strings in an array, use std::vector
instead.
std::vector<std::string> possible_strings;
std::string jumbled_chars; //This could be a char[] or char* or whatever
I'll leave figuring our exactly how to get all permutations of a string as an exercise for the reader. But say you want to get jumbled_chars
in the order of w
, x
, y
, z
, where w-z
are indices of jumbled_chars
:
std::string str = "";
str += jumbled_chars[w];
str += jumbled_chars[x];
str += jumbled_chars[y];
str += jumbled_chars[z];
possible_strings.push_back(str);
Upvotes: 1
Reputation: 740
#include <iostream>
#include <string>
using namespace std;
int main()
{
string theString = "";
char a = 'a';
char b = 'b';
const char* c = "cdefghijklmnopqrstuvwxyz";
theString += a;
theString += b;
theString += c;
cout << theString;
return 0;
}
That prints out the entire alphabet.
Upvotes: 7