[ create a new paste ] login | about

Link: http://codepad.org/sS79cznk    [ raw code | output | fork ]

mohit_at_codepad - C, pasted on Jun 12:
// Reverse all vowels in a string
#include <stdio.h>
#include <string.h>
#include <ctype.h>

int isVowel(char ch) {
  const char cha = tolower(ch);
  switch(cha) {
    case 'a': case 'e': case 'i': case 'o': case 'u': return 1;
  }
  return 0;
}

void swap_(char *a, char *b) {
  char temp = *a;
  *a = *b;
  *b = temp;
}

void printReverse(char *a) {
  int len = strlen(a) - 1;
  int i = 0;
  while(i < len) {
    while( (i < len) && (!isVowel(a[i]) ) ) ++i;
    while( (i < len) && (!isVowel(a[len]) ) ) --len;
    if(i < len) {
      char t = a[i];
      a[i] = a[len];
      a[len] = t;
    }
    ++i, --len;
  }
  printf(a);
}

int main() {
  char name[] = "MohitJain";
  printReverse(name);
  return 0;
}


Output:
1
MihatJion


Create a new paste based on this one


Comments: