×

C Programs

C Basic & Conditional Programs

C Looping Programs

C String Programs

C Miscellaneous Programs

C program to eliminate/remove all vowels from a string

Here, we will learn how to eliminate/remove all vowels from a string?, in this program we are reading a string of 100 (maximum) characters, and printing the string after eliminating vowels from the string.

Functions Created

Here, we designed two user defined functions:

1) char isVowel(char ch)
This function will take a single character as an argument and return 0 if character is vowel else it will return 1.

2) void eliminateVowels(char *buf)
This function will take character pointer (input string) as an argument and eliminate/remove all vowels in it.

Logic to eliminate/remove all vowels from a string

  1. Run a parent loop from 0 to NULL.
  2. Chech the character, whether it is vowel or not, if the character is vowel shift all characters to the left.
  3. To shift all characters to the left, run another loop from "i" (Parent loop's loop counter) to NULL.

Program to eliminate/remove all vowels from an input string in C

#include <stdio.h>
#define MAX 100

// function prototypes

/*
This function will return o if 'ch' is vowel
else it will return 1
*/
char isVowel(char ch);

/*
This function will eliminate/remove all vowels
from a string 'buf'
*/
void eliminateVowels(char *buf);

/*main function definition*/
int main() {
  char str[MAX] = {0};

  // read string
  printf("Enter string: ");
  scanf("%[^\n]s", str);  // to read string with spaces

  // print Original string
  printf("Original string: %s\n", str);

  // eliminate vowles
  eliminateVowels(str);
  printf("After eliminating vowels string: %s\n", str);

  return 0;
}

// function definitions

char isVowel(char ch) {
  if (ch == 'A' || ch == 'a' || ch == 'E' || ch == 'e' || ch == 'I' ||
      ch == 'i' || ch == 'O' || ch == 'o' || ch == 'U' || ch == 'u')
    return 0;
  else
    return 1;
}

void eliminateVowels(char *buf) {
  int i = 0, j = 0;

  while (buf[i] != '\0') {
    if (isVowel(buf[i]) == 0) {
      // shift other character to the left
      for (j = i; buf[j] != '\0'; j++) buf[j] = buf[j + 1];
    } else
      i++;
  }
}

Output

Enter string: Hi there, how are you?
Original string: Hi there, how are you? 
After eliminating vowels string: H thr, hw r y?

C String Programs »

Related Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.