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

#345 Reverse Vowels of a String

时间:2016-05-05 22:42:02      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:

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".

 

题意:将一个字符串中a,e,i,o,u收尾互换位置,返回。

 

class Solution {
public:
    string reverseVowels(string s) {
        int len = s.length();
        char temp;
        for(int i = 0, j = len - 1; i < j; i++) {
            if(s[i] == ‘a‘ || s[i] == ‘e‘ || s[i] == ‘i‘ || s[i] == ‘o‘ || s[i] == ‘u‘ ||s[i] == ‘A‘ || s[i] == ‘E‘ || s[i] == ‘I‘ || s[i] == ‘O‘ || s[i] == ‘U‘) { 
            	for(j; j > i; j--)
            	if(s[j] == ‘a‘ || s[j] == ‘e‘ || s[j] == ‘i‘ || s[j] == ‘o‘ || s[j] == ‘u‘ ||s[j] == ‘A‘ || s[j] == ‘E‘ || s[j] == ‘I‘ || s[j] == ‘O‘ || s[j] == ‘U‘) {
					temp = s[i];
					s[i] = s[j];
					s[j] = temp;
					j--;
					break;
				}
			} 
		} 
        return s;
    }
};

  

#345 Reverse Vowels of a String

标签:

原文地址:http://www.cnblogs.com/xiaohaigege/p/5463432.html

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