字符串

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<cstdlib>
using namespace std;
int main()
{
	int repeat,sum,i,j,num,s=0;
	string st;
	char ch;
	cin>>repeat;
	for(i=0;i<repeat;i++)
	{
		cin>>ch>>st;
		sum=strlen(st.c_str());//写成sum=strlen(st)过不了(错误原因在于st是一个std::string类型的变量,而strlen要求的参数是char*类型的
  //如果要获取st字符串的长度,std::string自带一个size接口可以满足需求
  //如果仍要继续使用strlen,则可以使用std::string的c_str接口。该接口返回一个const char*指针,正好可以作为参数传递给strlen)
		for(j=sum;j>=0;j--)
		{
			if(ch==st[j])
			{
				num=j;
				cout<<"index "<<"= "<<num<<endl;
				s=1;
				break;
			}
		}
		if(s!=1)
		{
			cout<<"Not Found"<<endl;
		}
		s=0;
	}
	return 0;
 }
posted @ 2016-06-01 20:11  埃尼阿克  阅读(110)  评论(0编辑  收藏  举报