Responsive Advertisement

Wednesday, September 4, 2024

Ai in C/C++ Code

 This Code is to make a Very Basic Ai in C









CODE :


#include <stdio.h>

#include <string.h>

#include <ctype.h>


// Function to convert string to lowercase

void to_lowercase(char str[]) {

    for (int i = 0; str[i]; i++) {

        str[i] = tolower(str[i]);

    }

}


// Function to check if a substring exists in a string

int contains_word(const char *str, const char *word) {

    return strstr(str, word) != NULL;

}


// Function to check user input and respond

void check_input(char user_input[]) {

    to_lowercase(user_input);


    if (contains_word(user_input, "hello")) {

        printf("Hi there!\n");

    } else if (contains_word(user_input, "bye")) {

        printf("Goodbye!\n");

    } else if (contains_word(user_input, "hi")) {

        printf("hi there!\n");

    } else if (contains_word(user_input, "yo")) {

        printf("yo yo hi there\n");

    } else if (contains_word(user_input, "song")) {

        printf("i am currently in beta stage so I can't speak or write songs\n");

    } else if (contains_word(user_input, "kida")) {

        printf("vadia yar tu ds\n");

    } else if (contains_word(user_input, "kidaa")) {

        printf("vadia yar tu ds kidaa?\n");

    } else if (contains_word(user_input, "kidda")) {

        printf("vadia yar tu ds kidaa?\n");

    } else if (contains_word(user_input, "song")) {

        printf("here you go: I wrote a song for you here it is --> Song :)\n");

    } else if (contains_word(user_input, "write")) {

        printf("Sure I can write WRITE for you Here it is: Write\n");

    } else if (contains_word(user_input, "kive aw")) {

        printf("vadia bs chali janda\n");

    } else if (strspn(user_input, "0123456789") == strlen(user_input)) {

        printf("You entered a number: %s\n", user_input);

    } else {

        printf("I don't understand that input.\n");

    }

}


int main() {

    char user_input[100];


    while (1) {

        printf("Enter something: ");

        fgets(user_input, sizeof(user_input), stdin);

        user_input[strcspn(user_input, "\n")] = '\0'; // Remove newline character


        check_input(user_input);


        // Exit the loop if the user types 'bye'

        if (contains_word(user_input, "bye")) {

            break;

        }

    }


    return 0;

}


No comments:

Post a Comment