指针原来是套娃的


私信TA

用户名:uq_92467646842

访问量:42945

签 名:

数学改变科学,科学改变世界

等  级
排  名 10
经  验 25103
参赛次数 49
文章发表 128
年  龄 0
在职情况 学生
学  校
专  业 物联网工程

  自我简介:

QQ:2830671713

解题思路:

stl里面的string类提供了查找函数find 我们只需要建立一个元音字母表,然后在表内查询就可以了。

参考代码:

#include <bits/stdc++.h>

#define ll long long
using namespace std;

int main()
{
	string s="aeiou";
	string k;
	cin>>k;
	for(int i=0;i<k.size();i++){
		if(s.find(k[i])!=s.npos)cout<<k[i];//find函数如果找不到会返回一个s.npos
	}
    
    return 0;
 }

find函数的具体用法为:

void test()
{
    string s("dog bird chicken bird cat");

    //字符串查找-----找到后返回首字母在字符串中的下标

    // 1. 查找一个字符串
    cout << s.find("chicken") << endl;        // 结果是:9

    // 2. 从下标为6开始找字符'i',返回找到的第一个i的下标
    cout << s.find('i',6) << endl;            // 结果是:11

    // 3. 从字符串的末尾开始查找字符串,返回的还是首字母在字符串中的下标
    cout << s.rfind("chicken") << endl;       // 结果是:9

    // 4. 从字符串的末尾开始查找字符
    cout << s.rfind('i') << endl;             // 结果是:18-------因为是从末尾开始查找,所以返回第一次找到的字符

    // 5. 在该字符串中查找第一个属于字符串s的字符
    cout << s.find_first_of("13br98") << endl;  // 结果是:4---b

    // 6. 在该字符串中查找第一个不属于字符串s的字符------先匹配dog,然后bird匹配不到,所以打印4
    cout << s.find_first_not_of("hello dog 2006") << endl; // 结果是:4
    cout << s.find_first_not_of("dog bird 2006") << endl;  // 结果是:9

    // 7. 在该字符串最后中查找第一个属于字符串s的符
    cout << s.find_last_of("13r98") << endl;               // 结果是:19

    // 8. 在该字符串最后中查找第一个不属于字符串s的字符------先匹配t--a---c,然后空格匹配不到,所以打印21
    cout << s.find_last_not_of("teac") << endl;            // 结果是:21

}

image.png

 

0.0分

165 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答

代码解释器

  评论区

改进了一下了楼上的
void tiqu(char a[100])
{
     int i;
     for(i=0;i<strlen(a);i++)
    {
        if(a[i]=='a'||a[i]=='e'||a[i]=='i'||a[i]=='o'||a[i]=='u')
        printf("%c",a[i]);
    }
}

#include<stdio.h>
int main()
{
    char a[100];
    int i;
    gets(a);
    tiqu(a);


	return 0;
}
2023-02-03 19:00:04
#include<stdio.h>
int main()  
{
    char a[100];
    int i;
    gets(a);
    
    for(i=0;i<strlen(a);i++)
    {
        if(a[i]=='a'||a[i]=='e'||a[i]=='i'||a[i]=='o'||a[i]=='u')
        printf("%c",a[i]);
    }
	return 0;
}
2022-11-10 22:14:34
  • «
  • 1
  • »