Write a C++ program that defines an array of 10 characters ( only lower-case letters ) then you have to check if character 'a' oppears within the array by outputting the index of the character of it is found otherwise output " the character is not found " .
It is not mentioned whether the array of 10 characters are taken from user or given directly. So lets assume that these 10 characters are taken from user. Also it is not mentioned whether these 10 characters are distinct (or) not. So lets consider these are not distinct which means 'a' can appear zero(or) more number of times in array. The C++ program of the above scenario is given below :
#include <iostream>
using namespace std;
int main()
{
int i=0,count=0;
char arr[10];
cout<<"Enter the lower-case characters"<<"\n";
cin>>arr;
for(i=0;i<10;i++)
{
if(arr[i]=='a')
{
cout<<"'a' is found at index "<<i<<"\n";
count++;
}
}
if(count==0)
cout<<"The character is not found"<<"\n";
return 0;
}
The screen shot of compiled code is given below :
The output when all the characters are distinct is shown below :
The output when the characters are not distinct is shown below :
The output when the character 'a' is not present is shown below :
Get Answers For Free
Most questions answered within 1 hours.