c program that replace vowels in a string with char

Here is a C program that replaces vowels in a string with a specified character:

#include <stdio.h>
#include <string.h>

int isVowel(char c) {
    char vowels[] = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'};
    for (int i = 0; i < 10; i++) {
        if (c == vowels[i]) {
            return 1;
        }
    }
    return 0;
}

void replaceVowels(char *str, char ch) {
    int len = strlen(str);
    for (int i = 0; i < len; i++) {
        if (isVowel(str[i])) {
            str[i] = ch;
        }
    }
}

int main() {
    char str[100];
    char ch;
    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);
    printf("Enter a character to replace vowels with: ");
    scanf("%c", &ch);
    replaceVowels(str, ch);
    printf("String after replacing vowels: %s", str);
    return 0;
}

Explanation:

  1. The program includes the necessary header files stdio.h and string.h for input/output and string manipulation functions.
  2. The function isVowel is defined to check if a given character is a vowel. It takes a character c as input and compares it with a list of vowel characters using a loop. If a match is found, it returns 1 (true), otherwise it returns 0 (false).
  3. The function replaceVowels is defined to replace vowels in a given string with a specified character. It takes a string str and a character ch as input. It calculates the length of the string using the strlen function and then iterates through each character of the string. If a character is a vowel (determined using the isVowel function), it is replaced with the specified character ch.
  4. The main function is the entry point of the program. It declares a character array str to store the input string, and a character ch to store the replacement character. It prompts the user to enter a string using printf and fgets, and prompts the user to enter a character using printf and scanf. It then calls the replaceVowels function to replace the vowels in the string, and finally prints the modified string using printf.

This program uses two functions, isVowel and replaceVowels, to check if a character is a vowel and replace vowels in a string, respectively. The main function handles input and output and orchestrates the overall execution of the program.