c++ - 如何删除“继续;”?
问题描述
我有这个 C++ 程序,它试图从 char 数组中删除元音。这可行,但我想在没有continue;
.
#include <iostream>
#include <string.h>
using namespace std;
int main() {
char s[21],afterElimination[19],vowels[] = "aeiou";
cin.get(s,20);
int i, n = strlen(s),VowelPos = -1,h = 0;
for (i = 0; i < n; i++) {
if (strchr(vowels,s[i])) {
if(VowelPos == -1) {
VowelPos = i;
continue;
}
VowelPos = i - 1;
}
afterElimination[h++] = s[i];
}
afterElimination[h] = NULL;
strcpy(afterElimination + VowelPos, afterElimination + VowelPos + 1);
cout<<afterElimination;
return 0;
}
解决方案
continue
从循环中删除非常容易。您需要的是循环中的两个索引。一个用于源数组中的位置,另一个用于要复制到的数组中的位置。然后将复制操作放在 if 语句中,因此如果没有复制,则无需执行任何操作以进行下一次迭代。那会让你循环看起来像
for (int source_index = 0, copy_index = 0; source_index < n; ++source_index) { // increment source_index always
if (!strchr(vowels,s[i])) {
// this isn't a vowel so copy and increment copy_index
afterElimination[copy_index++] = s[i];
}
// now this is the continue but we don't need to say it
}
推荐阅读
- ios - 使用 iCloud 进行应用程序数据库备份,但不同步
- git - 为什么 git 不让我推送我的更改?
- html - 使用 Flexbox 的等高卡片
- python-3.x - python - 如何在带有硒的python中获取具有某些标准的文本?(与某些兄弟姐妹的文本)
- ios - 如何使圆形收集单元适合所有屏幕?
- javascript - 如果条件是同步的还是异步的,内部调用函数?
- outlook - Outlook 2016 是否仍支持 Outlook.Application 组件?
- laravel - 如何在 laravel 项目的 vuejs 模板中的 select 标签内使用 optgroup 两次?
- python - 将类的自身命名为变量名
- python-3.7 - 如何使用 anaconda 中的一些代码在 python 中安装 YoloV3?