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

本文介绍了一种字符串处理的方法,特别关注于如何通过编程手段实现字符串中元音字母的翻转。文中给出的示例代码展示了如何使用C语言来完成这一任务,包括设置左右指针、定义元音字符数组等步骤。

字符串中的元音字母翻转

345. Reverse Vowels of a String

 
QuestionEditorial Solution
  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

 

转载于:https://www.cnblogs.com/nm90/p/5700133.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值