Monday 2 December 2013

Remove vowels string c

C programming code

#include <stdio.h>
#include <string.h>
 
int check_vowel(char);
 
int main()
{
  char s[100], t[100];
  int i, j = 0;
 
  printf("Enter a string to delete vowels\n");
  gets(s);
 
  for(i = 0; s[i] != '\0'; i++) {
    if(check_vowel(s[i]) == 0) {       //not a vowel
      t[j] = s[i];
      j++;
    }
  }
 
  t[j] = '\0';
 
  strcpy(s, t);    //We are changing initial string
 
  printf("String after deleting vowels: %s\n", s);
 
  return 0;
}
 
 
int check_vowel(char c)
{
  switch(c) {
    case 'a':
    case 'A':
    case 'e':
    case 'E':
    case 'i':
    case 'I':
    case 'o':
    case 'O':
    case 'u':
    case 'U':
      return 1;
    default:
      return 0;
  }
}

C programming code using pointers

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define TRUE 1
#define FALSE 0
 
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 is \"%s\"\n", string);
 
   return 0;
}
 
int check_vowel(char a)
{
   if ( a >= 'A' && a <= 'Z' )
      a = a + 'a' - 'A';
 
   if ( a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u')
      return TRUE;
 
   return FALSE;
}

No comments:

Post a Comment