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

  1. 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”.
  2. Code Structure:
    • The chatbot_response function compares the user input to predefined responses using strcmp().
    • 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, and strcspn is used to remove the newline character that fgets adds at the end of the input.
  3. How to Run:
    • Save the code as a .c file and compile it using the gcc compiler.
    • The compiled program can be executed in the terminal or command prompt.

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:

  1. Open a text editor and paste the provided C code into a new file.
  2. Save the file with a .c extension, for example, simple_chatbot.c.
  3. Open a terminal or command prompt on your computer.
  4. Navigate to the folder where the C file is saved.
  5. Compile the C program using a C compiler like gcc:
    gcc simple_chatbot.c -o simple_chatbot
  6. Run the compiled program:
    ./simple_chatbot
© 2024 Learn Programming. All Rights Reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)