Write a function printNthCharacter that prints the nth character from every stri
ID: 3587510 • Letter: W
Question
Write a function printNthCharacter that prints the nth character from every string in an array of strings. It should take as input: an array of strings words an integer size for the size of the array an integer n for the position of the character to be printed from each string It should print the nth characters of the strings in the array Example: For this input: ["hey you", "echo", "loyal", "little", "orange", n- We expect this output: hello Since these are the nth(Oth) characters of the strings in the array: ("hey you", "echo", "loyal", "little", "orange") Start by writing the function to print out all the strings in their entirety. (hey youecholoyallittleorange). Then modify it so that only the nth character of each string is printed (hello). Answer: (penalty regime: 0 %) 1 string printNthCharacter(string wordsC], int size, int n) 4Explanation / Answer
#include <iostream>
using namespace std;
string printNthCharacter(string words[], int size, int n)
{
for(int i = 0; i < size; i++)
{
if (words[i].length() > n) {
cout << words[i][n];
}
}
cout << endl;
return "";
}
int main()
{
string words[] = {"hey you", "echo", "loyal", "little", "orange"};
printNthCharacter(words, 5, 0);
return 0;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.