Help with making uppercase words (C++)
I'm working on a program that takes in a word, say 'test'. Then it will output every combination of lowercase and uppercase letters it can out of that word. For example, using 'test', we can make all of these:
test
Test
tEst
teSt
tesT
TEst
TeSt
TesT
tESt
tEsT
teST
TeST
TEsT
TESt
tEST
TEST
The amount of possible words you can make is 2 (1 for uppercase and 1 for lowercase of each letter) to the power of the words size. Since 'test' is 4 characters long, its 2^4, which is 16, if it was a word like 'other', it would be 2^5, which is 32, etc...
The problem I am having is that I can't rattle my brain around a solution to doing this for any word entered. If anyone has any ideas, that'd be awesome.
Edit:
One way would be to make an integer for each letter in the word. This is going to be hard to explain so bear with me...
So with 'test', we have 4 characters, that means 4 variables.
The first variable will help us create single-uppercased letters, like so (each color is a different variable):
Test
tEst
teSt
tesT
You see how it just moves on down the word...
Now let me show you it with two uppercased letters:
TEst
TeSt
TesT
tESt
tEsT
teST
Now here you can see that each variable, green and red, moves along the word.
When a variable reaches the end, the variable before it moves down 1, and the variable that reached the end goes to the character after that variable. So in our example, when green reached the end, the red variable went from 'T' to 'E', and the green variable went to 'S'. Then when the green variable reached the end again, the red went to 'S', and the green went to the variable after the red one, which was 'T'. When all the variables have moved all the way forward, it's done.
Here are 3 uppercased letters:
TESt
TEsT
TeST
tEST
In that example, when the pink variable reached the end, the variable before it moved forward, which was the green one. The rule is basically, when a variable has reached the end, the variable (if any) before it moves forward 1 character, but if that character is at the end, the character before that moves forward 1 character, etc... Until all variables have reached the end.
And lastly, 4 uppercased letters:
TEST
Since all of the characters are already at the end, that is the only possible output.
Soooo, what this is saying, is that when given a word, the program will need to create X amount of variables, where X is the length of the word, then keep moving each variable forward until it either hits the end of the word, or it hits another variable. If all variables are either at the end of the word, or have hit another variable, there are no more combinations for that amount of upper-cased characters, then it will add 1 more variable to the mix to create 2, or 3, etc. upper-cased letters in the word.
So if anyone can tell me how to make integers on-the-fly in C++ that'd be great............................................. .....
More Edits:
Woo, 2 edits in 10 minutes!
Anyway, seeing as how creating a butt load of variables on the fly then just destroying them would take a lot of cpu power, I was thinking of making a vector array, and just change the vector's size to the size of the word (since vectors were made to be easily resizable), then using the above rules to iterate through the array.
Also, if I' not just talking to myself, even though it's 3:53 AM, somebody tell me I'm not crazy :) .
Another More Edits:
I was also thinking of making a class for each character that holds its postion in the word, if it's uppercase or not, and any other info I can think of. Also, making classes would be helpful, because if a word had special characters or numbers, you can't make them uppercase... so I could only make classes for letters, then iterate through each class.
Keeping You Updated:
For now, unless there is a faster way, I'm using a vector of integers as each variable, and I've made up some basic rules of 'collision':
- if variable is at the end of the word, and it's not the only variable, move the character before it down one, and go to the character after that variable
- if variable has hit another variable, and it's the last variable, then there are no more combinations
- if variable has hit another variable, and it's not the last variable, then the variable before it moves down 1 and the variable that collided moves to the character after that variable
- after checking everything, if word is still valid, move the farthest down variable over 1
Oh and by the way by 'down', I mean down the word:
<- up down ->
THISISALONGWORD
Pseudo Code:
Alright, this is my last post for tonight because now it's 5:22 AM and I'm getting up at 7:00 AM -.-
Anyway, I've thrown together some pseudo-code for the program since winging it didn't really work...
Code:
while the user wants to continue
while asking for a file
If file isn't open
Tell them it wasn't open
else if file is open
break from the loop
While not end of the file
read word from line
if word has no alpha characters in it
output word
else
make word all lower case
get word size
calculate for the possible combinations (2^word size)
for each amount of letters in the word (1-wordsize)
erase var vector and valid vector
for each alpha character in the word
push back each alpha character's location to var and valid
var size = vecsize
while word is still valid
reset word to lowercase
for each vector in var, make those position's characters upper case
output word to newfile/console
if variable has hit the end of the word or another variable, and is the last variable
word is no longer valid
else if variable has hit the end of the word, and is not the last variable
the variable before it goes down one, and the current variable goes 1 before that
Feel free to comment/edit/reply/add/help/criticize/rant/flame/quote/hug.