Write the following program in c++.
the project.
Combine your first and second name and pick ony the first five characters of your name for
i.Decare an array named name1 and put al the first five characters into it.
searched character in the array. Let the program be such that the user will input the
ii.Write a for loop with an if condition that will find and output the index value of a
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