I can't seem to figure out why the program below places the second "r" into the even string when the index is an odd number. I'm new to the debugger but it just showed me the steps. It doesn't explain why. I'm new to programming and am learning on my own so I don't have a teacher or classmates to help out. I appreciate anyone taking the time to help out. Thank you.
#include <iostream>
#include <string>
std::string evenOddString(std::string);
int main()
{
std::string txt("airforce");
txt = evenOddString(txt);
std::cout << txt << std::endl;
return 0;
}
std::string evenOddString(std::string txt)
{
std::string even{""}, odd{""}, result{""};
for (char c : txt)
{
if (txt.find(c) % 2 == 0)
{
even.push_back(c);
}
else
{
odd.push_back(c);
}
}
result = even + " " + odd;
return result;
}
Output:
arorc ife
Program ended with exit code: 0