游客 Signup | Login
中文 | En

3398 - 考试一 判断字符串中的字符

1.输入一个字符串str和一个字符ch,判断str中是否存在该字符;如果存在输出在该字符在串str中出现的次数和位置;

Input

hello world!

h

Output

1

Examples

Input

hello world!
l

Output

2 3 9 
3

Solution C

#include <stdio.h>

int main(int argc, const char * argv[]) {
    // insert code here...
    char str[20];
    char ch ;
    char c;
    int i = 0;
    int count = 0;
    gets(str);
    
    scanf("%c",&ch);

    c = str[i];
    while(c != '\0')
    {
        if(ch == c)
        {
            count++;
            printf("%d ",i);
        }
        c = str[++i];
    }
    printf("\n%d",count);
    return 0;
}

Solution C++

#include<iostream>
#include<string>

using namespace std;

int main()
{
	string s;
	char c;
	getline(cin,s);
	cin>>c;
	int sum=0;
	for(int i=0;i<s.length();++i)
	{
		if(s[i]==c)
		{
			cout<<i<<" ";
			sum++;
		}
	}
	cout<<endl;
	cout<<sum;
	return 0;
}
Time Limit 1 second
Memory Limit 128 MB
Discuss Stats
上一题 下一题