Introduction
A chatbot is an artificial intelligence program designed to simulate conversation with users.
In this tutorial, we will create a simple chatbot using the C programming language. This chatbot
will respond to predefined questions with specific answers. It’s a basic implementation that can be
expanded upon to make more advanced interactive chatbots.
Objective
The main objective of this program is to create a simple chatbot that can engage in basic conversation
with the user. The chatbot will provide responses based on specific user input. The user will interact
with the chatbot through the command line interface, where they can ask predefined questions.
Code Implementation
#include #include // Function to display the chatbot's response void chatbot_response(char user_input[]) { if (strcmp(user_input, "hello") == 0) { printf("Chatbot: Hi! How can I help you today?\n"); } else if (strcmp(user_input, "how are you?") == 0) { printf("Chatbot: I'm just a program, but I'm doing fine. How about you?\n"); } else if (strcmp(user_input, "bye") == 0) { printf("Chatbot: Goodbye! Have a nice day!\n"); } else { printf("Chatbot: Sorry, I don't understand that.\n"); } } int main() { char user_input[100]; printf("Chatbot: Hello! Type 'bye' to exit.\n"); // Loop to keep the chatbot running while (1) { printf("\nYou: "); fgets(user_input, sizeof(user_input), stdin); // Remove newline character from the input user_input[strcspn(user_input, "\n")] = 0; // If user types "bye", exit the loop and end the program if (strcmp(user_input, "bye") == 0) { chatbot_response(user_input); break; } // Respond to the user input chatbot_response(user_input); } return 0; }
Explanation of the Program
- Introduction & Objective: The chatbot is a basic interactive program written in C, which takes user input and gives predefined responses based on specific keywords like “hello”, “how are you?”, and “bye”.
- Code Structure:
- The
chatbot_response
function compares the user input to predefined responses usingstrcmp()
. - The program continuously asks for user input in a loop. If the input is “bye”, the program exits.
- The
fgets
function is used to take user input, andstrcspn
is used to remove the newline character thatfgets
adds at the end of the input.
- The
- How to Run:
- Save the code as a
.c
file and compile it using thegcc
compiler. - The compiled program can be executed in the terminal or command prompt.
- Save the code as a
Program Structure and Explanation
This program is structured with a function chatbot_response
that takes the user’s input as
a string and matches it against predefined responses. It uses the strcmp
function to compare
the user’s input with specific phrases such as “hello”, “how are you?”, and “bye”. Depending on the user’s
input, the chatbot responds accordingly.
The program’s main function contains a while
loop, which keeps the chatbot running. The user
types their input, and the program reads this input using fgets
. If the input matches the exit
condition (i.e., “bye”), the loop breaks and the chatbot stops. Otherwise, the program calls the
chatbot_response
function to display the corresponding response.
How to Run the Program
To run this program, follow these steps:
- Open a text editor and paste the provided C code into a new file.
- Save the file with a .c extension, for example, simple_chatbot.c.
- Open a terminal or command prompt on your computer.
- Navigate to the folder where the C file is saved.
- Compile the C program using a C compiler like gcc:
gcc simple_chatbot.c -o simple_chatbot
- Run the compiled program:
./simple_chatbot