Answer to Question #282538 in C++ for Bismark Akyen

Question #282538

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

1
Expert's answer
2021-12-25T01:47:55-0500
#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!";
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog