File tree Expand file tree Collapse file tree 1 file changed +66
-0
lines changed Expand file tree Collapse file tree 1 file changed +66
-0
lines changed Original file line number Diff line number Diff line change 1+ /* 
2+  * Write a function that takes a string as input and reverse only the vowels of a string. 
3+  * 
4+  * Example 1: 
5+  * Given s = "hello", return "holle". 
6+  * 
7+  * Example 2: 
8+  * Given s = "leetcode", return "leotcede". 
9+  * 
10+  * Note: 
11+  * The vowels does not include the letter "y". 
12+  */  
13+ 
14+ #include  < iostream> 
15+ #include  < algorithm> 
16+ 
17+ bool  isVowel (char  c)
18+ {
19+     return  (c == ' a'   || c == ' e'   ||
20+             c == ' i'   || c == ' o'   ||
21+             c == ' u'   || c == ' A'   ||
22+             c == ' E'   || c == ' I'   ||
23+             c == ' O'   || c == ' U'  );
24+ }
25+ 
26+ std::string reverseVowels (std::string s)
27+ {
28+     if  (s.length () == 0 )
29+     {
30+         return  s;
31+     }
32+     
33+     unsigned  int  i = 0 ;
34+     unsigned  int  j = s.length () - 1 ;
35+     while  (i < j)
36+     {
37+         while  (i < j && !isVowel (s[i]))
38+         {
39+             i++;
40+         }
41+         
42+         while  (i < j && !isVowel (s[j]))
43+         {
44+             j--;
45+         }
46+         
47+         if  (i < j)
48+         {
49+             std::swap (s[i], s[j]);
50+             ++i;
51+             --j;
52+         }
53+     }
54+     
55+     return  s;
56+ }
57+     
58+ int  main ()
59+ {
60+     std::string str;
61+     std::cout << " Please enter input string: "  ;
62+     std::cin >> str;
63+     std::cout << " Expected output string: "   << reverseVowels (str) << std::endl;
64+ 
65+     return  0 ;
66+ }
    
 
   
 
     
   
   
          
     
  
    
     
 
    
      
     
 
     
    You can’t perform that action at this time.
  
 
    
  
     
    
      
        
     
 
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments