PDA

View Full Version : Qstring bad index.



gbtrl.
25th April 2021, 20:38
/*qstring object/* newText = arg1;

if(newText.size() > 0)
if(newText[newText.length()-1].isLetter())
{
for(int i{0}; i < vowels.length()-1; i++)
{
if(newText[newText.length()-1] == vowels[i])
{
newText[newText.length()-1] = vowels[i+1];
ui->lineEdit->setText(newText);
return;
}

vowels[i+1] indexes the last element of the Qstring, not the element one over from i. Why is this?
Also, is there a shorter way to reference the last element of a Qstring.

peppermg
25th April 2021, 23:47
What are you trying to accomplish? Is vowels a QString?

https://doc.qt.io/qt-5/qstring.html#back

gbtrl.
26th April 2021, 01:39
I am trying to overwrite the last character in newText with a specific character in vowels. Both are QString. The issue is vowels[i+1] always returns the last character of the string when it should return what is at the location of i+1.

d_stranz
26th April 2021, 16:40
It would help if you gave actual examples of what "newText" and "vowels" contain.

gbtrl.
26th April 2021, 19:48
Both contain regular alphabetical text. Lets say that newText = "HelloWorld", and vowels = "abifedonal". Using my code, when the 'e' in hello world is matched with the 'e' in vowels, it should
return 'd', because 'd' is contiguous to 'e'. It does not. It will always returns 'l', the last character of vowels.

d_stranz
26th April 2021, 22:48
Your code never tries to match anything with the "e" in HelloWorld. It is always looking a newText[ newText.length() - 1 ], the LAST character in newText, which is a 'd' in your example.



QString newText = "HelloWorld";
QString vowels = "abifedonal";

int len = newText.length();
if ( len > 0 )
{
QChar c = newText[ len - 1 ]; // contains a 'd'
if ( newText[ len - 1 ].isLetter() )
{
for ( int i{ 0 }; i < vowels.length() - 1; i++ )
{
if ( newText[ len - 1 ] == vowels[ i ] )
{
newText[ len - 1 ] = vowels[ i + 1 ];
break;
}
}
}
}


When this code hits the break statement on line 15, newText contains "HelloWorlo", which is exactly what the code is supposed to do. It matches the 'd' in "HelloWorld" with the 'd' in "abifedonal" and replaces it with the next character in vowels, an 'o'.

By the way, 'b', 'f', 'd', 'n', and 'l' are not vowels...

peppermg
26th April 2021, 22:59
Something like this?



QString vowels = "aeiouy";
QString newText = "helloworld!";

for (int i = 0; i < newText.size(); ++i) {
if (newText[i].isLetter()) {
int vIdx = vowels.indexOf(newText[i]);
if (vIdx != -1 && vIdx < vowels.size()-1) {
qDebug() << vIdx << newText[i] << vowels[vIdx+1];
newText[i] = vowels[vIdx+1];
}
}
}

qDebug() << newText;

d_stranz
26th April 2021, 23:31
Not quite. Your code will replace all vowels in newText, but the OP said this:


I am trying to overwrite the last character in newText with a specific character in vowels.

peppermg
27th April 2021, 00:08
Ah true, I probably should have read that a little closer.

d_stranz
27th April 2021, 00:22
And I realize that in combination with your indexOf() code, you could do it in about 2 or 3 lines.

gbtrl.
27th April 2021, 00:54
My bad. Replace the 'd' in "HelloWorld" with 'e'.

d_stranz
27th April 2021, 00:57
Replace the 'd' in "HelloWorld" with 'e'.

Well, that won't happen either with your example, because 'e' comes before 'd' in your vowels string.

gbtrl.
27th April 2021, 01:10
Just put one of vowels characters at the end of newtext and test it.

d_stranz
27th April 2021, 01:17
The code I posted earlier does exactly that. It replaces 'd' with 'o', the next character in the vowels string after 'd'.

Obviously the code you posted in your first post is not your real code, otherwise it would do the same.