Question

In: Computer Science

Define a function named "find" that accepts two input parameters: "s "and "ch". "s" is an...

  1. Define a function named "find" that accepts two input parameters: "s "and "ch". "s" is an object of the type "string" and "ch" is a character value with the default argument of 'A'. The function looks for the character "ch" in the string  "s" and displays all the indices of its occurrences on the screen. For example, if the caller sends the values of "ABCDBGAB" and 'B' to the function for the parameters "s" and "ch", respectively, the function displays the following message on the screen: "Character B is found at the following indices:1, 4, 7". If the character does not exist in the string the message would be: "Character B is found at the following indices: NONE".
  2. Write a program that first reads a string from the keyboard and then repeatedly reads a character from the keyboard and calls the "find" function implemented in the previous step to display all the occurrences of the input character in the string read from the keyboard, on the screen. The program terminates when the user enters a non-alphabetic character. It is assumed that no white-space character will be included neither in the input string nor in the sequence of characters entered to search; i.e. the white-space characters are used as separators of the input data only. A sample run of this program is given below:

Enter a string: "ABC8CBEABC+DE$$"

Enter a character to search: B

Character B is found at the following indices: 1, 5, 8

Enter a character to search: E

Character E is found at the following indices: 6, 12

Enter a character to search: w

Character w is found at the following indices: NONE

Enter a character to search: &

Solutions

Expert Solution

Code:

#include <iostream>

#include <vector>

using namespace std;

void find(string s, char ch)

{

printf("Character %c is found at the following indices: ", ch);

// We declare an Vector of type int to store the indices

vector<int> indices;

// We iterate over the string

for (int i = 0; i < s.length(); i++)

{

// If the character at index i in the string is equal to ch

if (s[i] == ch)

{

// Then we add the index i to the indices vector

indices.push_back(i);

}

}

// If no indices were found for the given character, then we print NONE

if (indices.size() == 0)

{

printf("NONE\n");

}

// Else we print the indices found for the given character

else

{

for (int i = 0; i < indices.size(); i++)

{

printf("%d", indices[i]);

if (i != indices.size() - 1)

{

printf(", ");

}

}

printf("\n");

}

}

int main()

{

printf("Enter a string: ");

// Reading the string from the user

string s;

cin >> s;

// This loop is iterated until an non-alphabetic character is entered by the user

while (true)

{

printf("Enter a character to search: ");

// Reading the character entered by the user

char ch;

cin >> ch;

// If it is alphabetic, then we call the find method.

if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'))

{

find(s, ch);

}

// Else we break the loop

else

{

break;

}

}

return 0;

}

Sample Output:


Related Solutions

C++ Please Define a function named "isAscending" that accepts a string as an input parameter and...
C++ Please Define a function named "isAscending" that accepts a string as an input parameter and returns "true" if all the characters included in the string are ordered in ascending order of their ASCII codes or the input string is a null string, and returns "false" otherwise. For example, if the string "ABXab" is passed to the function, it returns "true" because the ASCII code of 'B' is greater than 'A', 'X' is greater than 'B', 'a' is greater than...
Define a Python function named matches that has two parameters. Both parameters will be lists of...
Define a Python function named matches that has two parameters. Both parameters will be lists of ints. Both lists will have the same length. Your function should use the accumulator pattern to return a newly created list. For each index, check if the lists' entries at that index are equivalent. If the entries are equivalent, append the literal True to your accumulator. Otherwise, append the literal False to your accumulator. Hint: Since you must use the same index with each...
Define a function in Javascript named secondHighest which accepts an array of numbers, and returns the...
Define a function in Javascript named secondHighest which accepts an array of numbers, and returns the second highest number from the array. If the highest value occurs more than once, then it is also the second highest (see example). Assume the input array will always have length at least two (so there is always a second highest value). The original array must NOT be modified by this function. Example secondHighest([5,3,8,6,2,7,4]) must return 7, and secondHighest([5,3,8,6,2,7,8]) must return 8. I have...
java script Define a function named height which has two inputs. This first input is the...
java script Define a function named height which has two inputs. This first input is the number of feet. The second input is the number of inches. Both inputs will be Number values. Your function must calculate and return a Number. he returned value represents number of meters equal to the input. Your function should start by multiplying the first parameter by 0.3048 (1 foot is 0.3048 meters). Then multiply the second input by 0.0254 (1 inch is 0.0254 meters)....
Declare and define a function named getCurrentHours_OR_Month that accepts one Boolean parameter. If the Boolean parameter...
Declare and define a function named getCurrentHours_OR_Month that accepts one Boolean parameter. If the Boolean parameter is true, the function returns current hours; if the Boolean parameter is false, the function returns current month. o This function will obtain the current system time and extract the hours or the month value from the system time depending on the Boolean being received. o This function will return the extracted value as an integer value. o Note that the system time displays...
(java) Part 1 Write a method named compare that accepts two String arrays as parameters and...
(java) Part 1 Write a method named compare that accepts two String arrays as parameters and returns a boolean. The method should return true if both arrays contain the same values (case sensitive), otherwise returns false. Note - the two arrays must be the same length and the values must be in the same order to return true. Part  2 Write a method named generateArray that accepts an int size as its parameter and returns an int[] array where each element...
Define a function named sumsinconv that accepts double precision variable theta, double precision variable alpha, and...
Define a function named sumsinconv that accepts double precision variable theta, double precision variable alpha, and unsigned integer n, and returns the solution of expression sin ⁡ ( ( n + 1 ) α 2 ) sin ⁡ ( θ + n α 2 ) sin ⁡ ( α 2 ) in C Only show the function definition. Do not write an entire program with a main function. Just write the definition for function sumsinconv.
Write a program that contains the following Write a function copies with two int parameters, named...
Write a program that contains the following Write a function copies with two int parameters, named n and x. It should dynamically allocate a new array of size n.  The function should then copy the value in x to every position in the array. The function should return a pointer to the new array. In the main function, call the copies function from part 1. with size 5 and value -1.  Output the resulting array to the screen, and deallocate the array....
Write a function named "check_matrix" which takes two matrices as parameters and returns 1 if the...
Write a function named "check_matrix" which takes two matrices as parameters and returns 1 if the matrices are same or 0 otherwise. Set appropriate parameters and return type if necessary.
Write a recursive function named multiply that takes two positive integers as parameters and returns the...
Write a recursive function named multiply that takes two positive integers as parameters and returns the product of those two numbers (the result from multiplying them together). Your program should not use multiplication - it should find the result by using only addition. To get your thinking on the right track: 7 * 4 = 7 + (7 * 3) 7 * 3 = 7 + (7 * 2) 7 * 2 = 7 + (7 * 1) 7 *...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT