4069. 字符位置统计

问题描述

编写一个程序,输入一个字符串str(长度不超过30)和一个字符ch,统计字符串str中字符ch出现的位置(区分大小写)。

输入输出描述

输入

  • 输入一个字符串str,以及一个字符ch

输出

  • 若ch在str中存在,输出位置,位置之间以空格分隔
  • 若ch在str中不存在,输出NULL
  • 输出后面无换行符

程序运行示例1

Sample Input 1

Shanghaijiaotongdaxue h 

Sample Output 1

1 5

程序运行示例2

Sample Input 2

Helloworld @

Sample Output 2

NULL

注意

  • 不要显示多余的提示信息,避免输出判定错误
  • 注意判断输出信息是否符合要求。
  • 请尽量练习使用指针数组实现。
#include<iostream>
#include<cstring>
using namespace std;

int main(){
    string s;
    cin>>s;
    char a;
    cin>>a;
    bool flag=true;
    for(int i=0;i<s.size();i++){
        if(s[i]==a){
            flag=false;
            cout<<i<<" ";
        }
    }
    if(flag){
        cout<<"NULL";
    }
    return 0;
}
原文地址:https://www.cnblogs.com/bernieloveslife/p/7890573.html