输入一段字符串,删除其中'a','o','e','i','u','y'及它们的大写字母,同时将字符串中大写字母转换成小写字母并输出。用c语言和c++两种形式书写代码
时间: 2024-01-01 21:04:46 浏览: 136
C语言代码:
```c
#include <stdio.h>
#include <ctype.h>
#include <string.h>
void remove_vowels_and_lower(char *str) {
int len = strlen(str);
int i, j;
for (i = j = 0; i < len; i++) {
char ch = str[i];
if (ch >= 'A' && ch <= 'Z') {
ch = tolower(ch);
}
if (ch != 'a' && ch != 'e' && ch != 'i'
&& ch != 'o' && ch != 'u' && ch != 'y') {
str[j++] = ch;
}
}
str[j] = '\0';
}
int main() {
char str[100];
printf("请输入字符串:");
scanf("%s", str);
remove_vowels_and_lower(str);
printf("%s\n", str);
return 0;
}
```
C++代码:
```c++
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
void remove_vowels_and_lower(string &str) {
transform(str.begin(), str.end(), str.begin(), ::tolower);
str.erase(remove_if(str.begin(), str.end(), [](char ch) {
return ch == 'a' || ch == 'e' || ch == 'i'
|| ch == 'o' || ch == 'u' || ch == 'y';
}), str.end());
}
int main() {
string str;
cout << "请输入字符串:";
cin >> str;
remove_vowels_and_lower(str);
cout << str << endl;
return 0;
}
```
阅读全文