Monday, February 17, 2020

program to delete vowels from a string

programming code

#include <stdio.h>
#include <string.h>
int check_vowel(char);
int main()
{
  char s[100], t[100];
  int c, d = 0;
  printf("Enter a string to delete vowels\n");
  gets(s);
  for(= 0; s[c] != '\0'; c++) {
    if(check_vowel(s[c]) == 0) {       // If not a vowel
      t[d] = s[c];
      d++;
    }
  }
  t[d] = '\0';
  strcpy(s, t);  // We are changing initial string. This is optional.
  printf("String after deleting vowels: %s\n", s);
  return 0;
}
int check_vowel(char ch)
{
    if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' || ch =='o' || ch=='O' || ch == 'u' || ch == 'U')
      return 1;
    else
      return 0;
}
Output of program:
C program to delete vowels from a string output

C programming code using pointers

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

int check_vowel(char);

main()
{
   char string[100], *temp, *pointer, ch, *start;

   printf("Enter a string\n");
   gets(string);

   temp = string;
   pointer = (char*)malloc(100);

  if( pointer == NULL )
   {
      printf("Unable to allocate memory.\n");
      exit(EXIT_FAILURE);
   }
   start = pointer;

   while(*temp)
   {
      ch = *temp;

      if ( !check_vowel(ch) )
      {
         *pointer = ch;
         pointer++;
      }
      temp++;
   }
   *pointer = '\0';

   pointer = start;
   strcpy(string, pointer); /* If you wish to convert original string */
   free(pointer);

   printf("String after removing vowel(s): \"%s\"\n", string);

   return 0;
}

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

No comments:

Post a Comment