Reputation: 40
I want to take array of string from the user and output just those string indexes which starts from the character "A". Is there a way to do this?
#include <iostream>
#include <string>
using namespace std;
const int size=3;
void input(string name[size]);
void byname(string name[size]);
struct employee
{
string name[size];
}emp;
int main()
{
input(emp.name);
byname(emp.name);
return 0;
}
void input(string name[size]){
cout<<"Enter Names of the employees:\n";
for(int i=0;i<size;i++)
cin>>emp.name[i];
}
void byname(string name[size])
{
int i,j;
for(i=0;i<size;i++)
{
if(name[i]=="A")
cout<<"Name starting with A is "<<emp.name[i];
else if(name[i]=="a")
cout<<"Name starting with a is "<<emp.name[i];
}
}
Upvotes: 0
Views: 145
Reputation: 85
In c++, you can use array indexing to access individual characters of a string, more about c++ strings here: http://www.cplusplus.com/reference/string/string/ , Coming to your question, In that byname function , change the comparisons to :
if(name[i][0]=='A')
cout<<"Name starting with A is "<<emp.name[i];
else if(name[i][0]=='a')
cout<<"Name starting with a is "<<emp.name[i];
Do let me know if there are any other problems you are facing! Cheers!
Upvotes: 1