这是我的代码,希望采纳:
#include
using namespace std;
char* mystrrchr(char*str, char ch);
int main()
{
char str[100]="";
char c;
cout << "Please enter a string:";
cin >> str;
cout << "Please enter a char:";
cin >> c;
char*p = mystrrchr(str, c);
if (p == NULL)
{
cout << "Not Exsist!\n";
}
else
{
cout << mystrrchr(str, c) << "\n";
}
return 0;
}
char* mystrrchr(char*str, char ch)
{
char* res=NULL;
while (*str != '\0')
{
if (*str == ch)
{
res = str;
}
str++;
}
return res;
}
参考代码如下:
#include
char *mystrrchr(char *str,char ch)
{
int *p=NULL;
while(*str){
if(*str==ch)
p=str;
str++;
}
return p;
}
int main()
{
char ch,str[300],*p;
scanf("%s%*c%c",str,&ch);
p=mystrrchr(str,ch);
if(p!=NULL)
printf("%s",p);
else printf("不存在");
return 0;
}