I am trying to replace a substring of length 1 with a char - but obviously I cannot just stick a char in there. Can I do this on the fly? As in:
for (int j=0; j <= startword.size(); j++) {
for (char i='a'; i < 'z'; i++) {
choices.add(startword.replace(j, 1, string(i));
(but obviously not like that!)
Thanks for your help, this answer is not yet explicit on stackoverflow for c++ (I think only for java). Please excuse some n00bishness here, I am really giving it everything I promise.
Tyler
Well, in C++ the string is mutable and you can use the array operator:
for (int j=0; j < startword.size(); j++)
{
for (char i='a'; i < 'z'; i++)
{
string newChoice = startword;
newChoice[j] = i;
choices.add(newChoice);
}
}
EDIT: Cannot index newChoice[startWord.size()]
; changed the for loop condition.
Also you could do it like this (maybe useful if startword
is very long):
for (unsigned j=0; j < startword.size(); j++)
{
char saveChar = startword[j];
for (char i='a'; i <= 'z'; i++)
{
startword[j] = i;
choices.add(startword);
}
startword[j] = saveChar;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.