Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/143.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+中的字符串输出元音和辅音+;_C++_String_Search_Count_String Matching - Fatal编程技术网

C++ 从c+中的字符串输出元音和辅音+;

C++ 从c+中的字符串输出元音和辅音+;,c++,string,search,count,string-matching,C++,String,Search,Count,String Matching,这是我必须回答的问题: Write a program that declares two strings: s1 and s2. Initialize both of them using getline(cin, string) function. a) Output the length of each string b) Output the first appearance of a letter a in the first string c)

这是我必须回答的问题:

Write a program that declares two strings: s1 and s2. Initialize both of them using getline(cin, string) function. a) Output the length of each string b) Output the first appearance of a letter a in the first string c) Output the first appearance of a letter b in the second string d) Output the first word of each string e) Output the last word of each string f) Output first sentence reversed g) Output second sentence with the words reversed ( the last word goes first, second last second, and so on) h) Output the total number of vowels in the first sentence i) Output the total number of consonants in the second sentence 编写一个声明两个字符串的程序:s1和s2。 使用getline(cin,string)函数初始化它们。 a) 输出每个字符串的长度 b) 输出第一个字符串中字母a的第一个外观 c) 在第二个字符串中输出字母b的第一个外观 d) 输出每个字符串的第一个字 e) 输出每个字符串的最后一个字 f) 输出倒转的第一句话 g) 输出倒转单词的第二句话(最后一个单词排在第一位,第二个倒数第二位,依此类推) h) 输出第一句话中的元音总数 i) 输出第二句中辅音的总数 这就是我到目前为止所做的:

#include <iostream>
#include <string>

using namespace std;     

int main()  {
    string s1,s2,s3;
    int blank = 0;
    int counter1 = 0;
    int counter2 = 0;
    int counter3 = 0;
    int s2temp = 0;

    cout << "enter two sentences" <<endl;
    getline (cin, s1);
    getline (cin, s2);
    s3=s2;

    // a

    cout << "the length of the first string is " << s1.length() << endl;
    cout << "the length of the second string is " << s2.length() << endl;

    // b

    cout<<"the first appearance of the letter 'A' in the first string is ";
    cout << s1.find("a");
    cout <<endl;

    // c

    cout<<"the first appearance of the letter 'B' in the second string is ";
    cout << s2.find("b");
    cout <<endl;

    // d

    int s1_first = s1.find(" ");
    int s2_first = s2.find(" ");
    cout << "the first word in the first string is " << s1.substr(0,s1_first) <<endl;
    cout << "the first word in the second string is " << s2.substr(0,s2_first) <<endl;

    // e

    cout << "the last word in the first string is " << s1.substr(s1.find_last_of(" "), s1.length()-1) <<endl;
    cout << "the last word in the second string is " << s2.substr(s2.find_last_of(" "), s2.length()-1) <<endl;

    // f

    for(int i = s1.length()-1; i >= 0; i--)
        cout <<s1.substr (i,1)<<endl;

    // g

    return 0;
}
#包括
#包括
使用名称空间std;
int main(){
字符串s1、s2、s3;
int blank=0;
int计数器1=0;
int计数器2=0;
int计数器3=0;
int s2temp=0;

cout计算元音的一种方法是制作包含元音的字符串:

static const std::string vowels = "aeiouAEIOU";
接下来,对于字符串中的每个字符,在元音字符串中搜索它:

unsigned int vowel_count = 0;
const size_t length = text.length();
for (unsigned int i = 0; i < length; ++i)
{
  const char c = text[i];
  if (vowels.find(c) != std::string::npos)
  {
    ++vowel_count;
  }
}
unsigned int元音\u count=0;
const size_t length=text.length();
for(无符号整数i=0;i
这也适用于辅音

对于不允许使用
std::string
的用户,可以修改代码


另一种方法是使用
std::map

“…但我仍然无法得到它”-你得到了什么?你忽略了这一点。顺便说一句,调试器可以拯救生命(tm)。基本上每个任务都可以迭代字符串中的单个字符并相应地处理它们,这就像使用数组(如
if)一样(my_string[i]='')。
)有更复杂的方法来操作字符串,您可以使用搜索引擎阅读一些关于字符串的教程,但对于您的任务,我所说的就足够了。