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...
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...
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 *...
Write Java code that accepts the integer input (from keyboard) in an arraylist named num1 and...
Write Java code that accepts the integer input (from keyboard) in an arraylist named num1 and stores the even integers of num1 in another arraylist named evennum.
Write a function, named isMultipleOfFive that accepts integer argument. When the function is called, it should...
Write a function, named isMultipleOfFive that accepts integer argument. When the function is called, it should display if the argument "is a multiple of 5" or "is not a multiple of 5".
3. Write a function named "countNonAlpha" that accepts a string. It will return the number of...
3. Write a function named "countNonAlpha" that accepts a string. It will return the number of non-alphabet characters (excluding blanks) in the string. For example, if the string is "Hello, World!", it will return 2 for ',' and '!" in the string. 4. Write a function named "deleteZeros" that takes two arguments: a. an array of integer values; b. an integer for the number of elements in the array; The function will return the number of zeros that it has...
- Design and implement a function with no input parameters. The function keeps receiving a number...
- Design and implement a function with no input parameters. The function keeps receiving a number from input (user) and adds the numbers together. The application keeps doing it until the user enter 0. Then the application will stop and print the total sum and average of the numbers the user had entered.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT