problem with string.find()
i wrote a code to check the number of distinct char in a string whether even or odd using string.find(), but it seems it's not working !!!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
#include <iostream>
#include<string>
using namespace std;
int main()
{ int x=0,z;
string word;
cin >> word;
for(int j=0;j<word.size();j++)
for(int i=j+1;i<word.size();i++)
if(word.find(word[j],i)!=string::npos)
x++;
z=word.size()-x;
if (z%2==0) cout << "even";
else cout << "odd";
return 0;
}
| |
Please explain "not working".
I don't think that your code does exactly what you intend.
What should be the result with input "aaaa"?
Could you use std::map?
Topic archived. No new replies allowed.