Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 课文的最后一个辅音_C++ - Fatal编程技术网

C++ 课文的最后一个辅音

C++ 课文的最后一个辅音,c++,C++,用户输入文本(带空格)后,如何仅删除该文本的最后一个辅音 我不知道该怎么做,不管我现在做了什么,都删除了所有的辅音。你有没有尝试过什么,或者你不知道怎么做 我猜你什么都没做 首先,你必须找到最后一个辅音。你是怎么做到的。好的,你从结尾开始检查字母,直到找到一个辅音。 现在,当你找到最后一个辅音时,记住辅音的位置,然后将辅音右边的每个字母移到左边。 最后,将字符串中的最后一个字符更改为“\0” 代码如下: #include <iostream> #include <string&

用户输入文本(带空格)后,如何仅删除该文本的最后一个辅音


我不知道该怎么做,不管我现在做了什么,都删除了所有的辅音。

你有没有尝试过什么,或者你不知道怎么做

我猜你什么都没做

首先,你必须找到最后一个辅音。你是怎么做到的。好的,你从结尾开始检查字母,直到找到一个辅音。 现在,当你找到最后一个辅音时,记住辅音的位置,然后将辅音右边的每个字母移到左边。 最后,将字符串中的最后一个字符更改为“\0”

代码如下:

#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) = '\0';

    // 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;
}
#包括
#包括
#包括
布尔同调(char);
int main(int argc,char*argv[]){
chars[80];
//输入文本

std::你能说明你到目前为止尝试了什么以及为什么它不起作用吗?如果它只包含字母,或者如果它可能还包含其他字符,这可能是一个很好的使用案例。它可能有助于显示用于存储文本的数据结构:char[],std:array std::vector、std::string、list等。您是如何尝试接近它的>