LeetCode 345

Reverse Vowels of a String

Write a function that takes a string as input
and reverse only the vowels of a string.

Example 1:
Given s = "hello", return "holle".

Example 2:
Given s = "leetcode", return "leotcede".

 

 1 /*************************************************************************
 2     > File Name: LeetCode345.c
 3     > Author: Juntaran
 4     > Mail: JuntaranMail@gmail.com
 5     > Created Time: Tue 10 May 2016 08:55:33 PM CST
 6  ************************************************************************/
 7  
 8 /*************************************************************************
 9     
10     Reverse Vowels of a String
11     
12     Write a function that takes a string as input 
13     and reverse only the vowels of a string.
14 
15     Example 1:
16     Given s = "hello", return "holle".
17 
18     Example 2:
19     Given s = "leetcode", return "leotcede".
20 
21  ************************************************************************/
22 
23 #include <stdio.h>
24 
25 int isVowels( char s )
26 {
27     int flag = 0;
28     if( s=='a' || s=='e' || s=='i' || s=='o' || s=='u' || s=='A' || s=='E' || s=='I' || s=='O' || s=='U' )
29     {
30         flag = 1;
31     }
32     return flag;
33 }
34 
35 char* reverseVowels( char* s )
36 {
37     int left  = 0;
38     int right = strlen(s) - 1;
39     
40     printf("%c %c\n", s[left], s[right]);
41     char temp;
42     
43     
44     while( left < right )
45     {
46         printf("%d %d\n", left, right);
47         if( isVowels(s[left]) == 1 )
48         {
49             if( isVowels(s[right]) == 1 )
50             {
51                 temp = s[left];
52                 s[left] = s[right];
53                 s[right] = temp;
54                 
55                 left ++;
56                 right--;
57                 printf("%s\n", s);
58             }
59             else
60             {
61                 right--;
62             }
63         }
64         else
65         {
66             left ++;
67         }
68     }
69     return s;
70 }
71 
72 int main()
73 {
74     char* s = "leetcode";
75     printf("%s\n", s);
76     char* reverseS = reverseVowels(s);
77     printf("%s\n", s);
78     
79     
80     return 0;
81 }

 

posted @ 2016-05-11 17:41  Juntaran  阅读(225)  评论(0编辑  收藏  举报