Write the following program in c++.
Combine your first and second name and pick only the first five characters of your name for
the project.
i. Declare an array named name1 and put all the first five characters into it.
ii. Write a for loop with an if condition that will find and output the index value of a
searched character in the array. Let the program be such that the user will input the
character being searched for
#include<iostream>
#include<string>
using namespace std;
int main()
{
string fName,sName;
cout << "Please, enter your first name: ";
cin >> fName;
cout << "Please, enter your second name: ";
cin >> sName;
string fullName = fName + sName;
string firstFive = fullName.substr(0, 5);
char arr[5];
for (int i = 0; i < 5; i++)
{
arr[i] = firstFive[i];
}
char ch;
cout << "Please, enter the character being searched for array: ";
cin >> ch;
bool findChar = false;
for (int i = 0; i < 5; i++)
{
if (arr[i] == ch)
{
cout << "The index value of a searched character is " << i;
findChar = true ;
break;
}
}
if (!findChar)
{
cout << "The character does not exist!";
}
}
Comments
Leave a comment