find function not working (in vector)

After trying a lot I’m still not able to figure out why its not being able to find if reverse of the word being searched for exists in the vector already or not.
Here is the problem link :

Here is the code

#include <bits/stdc++.h>
using namespace std;

string rev(string s) //Function to reverse string
{
	string ans=" ";
	for(int i=s.length();i>=0;i--)
	ans = ans + s[i];
	return ans;
}
char print(string x)// Function that Returns the character at the middle position of a given string
{
	return x[(x.length()-1)/2]; 
} 
int main()
{
	    int n;
	    cin>>n;
	    vector<string>v;
	    string str;
	    for(int i=0;i<n;i++)
	    {
	    cin>>str;
	    v.push_back(str);
	    }
	    for(int i=0;i<n;i++)
	    {
	    if(find(v.begin(),v.end(),rev(v[i]))!=v.end()) // String found yo
	    {
	  	int len = v[i].length();
	  	cout<<len<<" "<<print(v[i])<<endl;
	  	break;
	  	}
	    }
    return 0;
}

Alternatively ideone link : e7T0FL - Online C++0x Compiler & Debugging Tool - Ideone.com

Thanks for reading :slight_smile:

The basic correction :

string rev(string s) //Function to reverse string
{
    string ans=""; // remove the space
    for(int i=s.length()-1;i>=0;i--)  // i = s.length() is wrong, the last index is s.length()-1
    ans = ans + s[i];
    return ans;
}

If at any point you are unsure of what the last position is, use rbegin() for iteration (complimented by r.end() ). Look it up on the c++ reference for details.

Well I guess that was a stupid mistake! Thanks alot :slight_smile:
Are there any suggestions you have on this code?