20 lines of c++ code to implement a fake chatbot.

Same old rule. Renderings first

#include<iostream>
using namespace std;
int main(a)
{
	while(1)
	{
		string s;
		cout<<"Real person:";
		cin>>s;
		if(s.find("?")! = string::npos) { s.replace(s.find("?"),3."!"	);
		}
		else
		{
			s=s+"!"; 
		}
		cout<<"Robot :"<<s<<endl; }}Copy the code

Take a look at the use of string in c++, which has two uses: find and replace.

S. ind (“?” )! = string::npos = string::npos = string::npos = string::npos

S.r eplace (s. ind (“?” ), 3, “!” ); Replace is the replacement, the corresponding three parameters are the position of the 1 substring, 2 is the replacement of several characters, because Chinese takes up two characters, so there are three characters, 3 is the replacement of the string.

Did you learn?