码迷,mamicode.com
首页 > 其他好文 > 详细

字符串中的元音字母翻转输出

时间:2016-07-24 10:29:30      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:

字符串中的元音字母翻转

345. Reverse Vowels of a String

 
 My Submissions
 
  • Total Accepted: 31038
  • Total Submissions: 86077
  • Difficulty: Easy

 

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. e 和 e 交换; 2. e 和 o交换

Note:
The vowels does not include the letter "y".

 

Subscribe to see which companies asked this question

技术分享
 1 char* reverseVowels(char* s) {
 2     
 3     int left,right;
 4     char tmp;
 5 
 6     char a[10]={a,e,i,o,u,A,E,I,O,U};
 7     char *tmp_a;
 8     tmp_a = a;
 9     int  i= 0;
10     
11     left = 0;
12     right = strlen(s)-1;
13     
14     
15     //两边同时移动指针赋值
16     while(left < right ){
17         tmp = s[left];
18         //左边判断
19         while(i < 10){
20             if(tmp_a[i++] == s[left]){
21                 i = 0;
22                 break;
23             }
24 
25         }
26         
27         if(i != 0){
28            left++;
29            i = 0;//回到起始位置
30            continue;
31         }
32         //右边判断  
33         while(i < 10){
34             if(tmp_a[i++] == s[right]){
35                 i = 0;
36                 break;
37             }  
38         }
39         
40         if(i != 0){
41            right--;
42            i = 0;//回到起始位置
43            continue;
44         }
45         
46         //tmp = s[left];
47         s[left] = s[right];
48         s[right] = tmp;
49         left++;
50         right--;
51         i = 0;
52     }
53 
54      return s;
55 
56 }
View Code

 

字符串中的元音字母翻转输出

标签:

原文地址:http://www.cnblogs.com/nm90/p/5700133.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!