Hi there :D
I'm just trying my luck on some challenges I found on the internet, but I'm starting to have problems with the count algorithm. I pretty much have my program set up to solve the challenge but the count algorithm does weird things:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
#include <iostream>
using namespace std;
#include <string>
#include <algorithm>
int main(){
bool iselimin=true;
string one;
string two;
cin >> one;
cin >> two;
int dieter;
one.append(two);
for(int a=0; a<one.size(); a++){
dieter=count(&one[0], &one[one.size()-1], one[a])%2;
if(dieter==0){
}
else if(dieter!=0){
iselimin=false;
}
cout << count(&one[0], &one[one.size()-1], one[a]) << endl;
}
if(iselimin==true){
cout << "Yes!";
}
else if(iselimin==false){
cout << "No!";
}
return 1;
}
|
line 15 uses the count function:
count(&one[0], &one[one.size()-1], one[a])
and the cout in line 22 (which was for test purposes) then printed out how many of each letter there where.
but oddly enough, when i have the following input "abc cba"
i get the output "1, 2, 2, 2, 2, 1" meaning that there only is one 'a' in the string???
the same happens with "bac cba" printing the following: "2, 1, 2, 2, 2, 1" meaning, that again there only is one 'a'...
this lead me to think that the count function in my case doesn't seem to acknowledge the last cell of the string for some reason.
I don't really know what to do in this situation. Is there any way to fix this?
greetings