Checking for Characters in an Array

Let's say I had an array of different names, and I wanted to check if the name started with a particular letter, which in this case is 'J'. How would I check for that? I wrote most of the source code (and created an array so that it'll print out inputted names that start w/ 'J' but I'm not entirely sure if I would just need a condition to check or not.

#include <iostream>
using namespace std;

int main()
{
string a[10];
string b[10];
int i=0, n=0, k=0;
cout<<"Enter the number of elements."<<endl;
cin>>n;
for(i=0;i<n;i++)
{
cout<<"\nA["<<i<<"]=";
cin>>a[i];
}

for(i=0;i<n;i++)
{
if/while/for()
{
b[k]=a[i];
k++;
}
}

for(int i=0;i<k;i++)
{
cout<<b[i]<<endl;
}
system("pause");
return 0;
}
Last edited on
You can reach the first character of std::string name through name[0], so in your case it'd be a matter of checing if name[0] == 'J', etc
Topic archived. No new replies allowed.