Answer to Question #283800 in C++ for Samuel Smith

Question #283800

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.

1
Expert's answer
2021-12-30T15:17:50-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