文本的最后一个辅音
Last consonant of a text
用户输入文本(带空格)后,如何仅删除该文本的最后一个辅音?
我不知道怎么做,不管我现在做了什么,把所有的辅音都删掉了
你尝试过什么吗?或者你不知道怎么做?
我假设你什么也没做。
首先,你必须找到最后一个辅音。你是怎么做到的?嗯,你从结尾开始检查字母,直到你找到一个辅音。现在,当你找到最后一个辅音时,你记住辅音的位置,然后把在辅音右边的每个字母移到左边。最后,将字符串中的最后一个字符更改为' '。
代码如下:
#include <iostream>
#include <string>
#include <cctype>
bool isConsonant(char);
int main (int argc, char *argv[]) {
char s[80];
// Input the text
std::cout << "Enter the text: ";
std::cin.getline(s,80,'n');
// Creating string object
std::string text(s);
int length = text.length();
// Searching for the last consonant
int i = length - 1;
while ((!isConsonant(text.at(i)) && (i)))
i--;
// Moving every letter after the last consonant one place to the left
for (int j = i; j < length - 1; j++)
text.at(j) = text.at(j+1);
text.at(length-1) = ' ';
// Printing the text to the standard output
std::cout << text;
return 0;
}
bool isConsonant(char c) {
if (isalpha(c)) {
char temp = c;
if (isupper(c))
temp = tolower(c);
return !((temp == 'a') || (temp == 'e') || (temp == 'i') || (temp == 'o') || (temp == 'u'));
}
return false;
}
编辑:当然还有其他方法可以做到这一点,你应该试着想想。这是我想到的第一件事。
相关文章:
- lower_bound()返回最后一个元素
- C++试图读取一个文件并输出到另一个文本文件
- 获取向量C++中第一个值和最后一个值的和
- 获取用C/C++打印的最后一个字符串
- 使用运算符 [] 引用 std::vector 上最后一个元素时出现问题<>
- 为什么这个程序返回最后一个单词而不是最长的单词?
- 为什么我的最后一个 ELSE 条件无法正确执行
- Lower_bound不适用于具有 3 个元素的向量的最后一个元素
- 我的C++程序只写入文本文件最后一个条目
- 如何确定文本中的最后一个字符串值
- C++ 输出在读取文本文件时跳过最后一个单词
- 如何将文本(诗歌)拆分为行(字符串/字符[])并查找每个行的最后一个单词
- 为什么当我在Visual c ++ studio 2010中运行应用程序时光标会转到最后一个文本框
- 获取分隔符为/的给定字符串的最后一个目录的文本
- 如何删除文本文件C++中的最后一个字符
- 读取文本文件并在C/C++中每行只得到最后一个整数的结果
- 最后一个单词在文本文件C++中重复
- 屏幕没有清除最后一个文本为什么?
- 如何在读取文本文件时跳到每行最后一个数字之后的下一行
- 文本的最后一个辅音