Design a class to overload a function print ( ) as follows:
(a) void print (int x, int y,char c)– to find the average of x and y if c is ‘a’
otherwise find the bigger of the two.
(b) void print (char ch1, char ch2) – to print the difference between the ASCII
codes of the two characters.
(c) void print (char ch, int n)– to print the character ch, n number of times in the
given format: Eg.: If ch = ‘$’ and n = 4 Output:
$ $ $ $
$ $ $
$ $
$
Answers
Answer:
// C++ program for above implementation
#include <iostream>
using namespace std;
// Function to print the string
void printString(string str, char ch, int count)
{
int occ = 0, i;
// If given count is 0
// print the given string and return
if (count == 0) {
cout << str;
return;
}
// Start traversing the string
for (i = 0; i < str.length(); i++) {
// Increment occ if current char is equal
// to given character
if (str[i] == ch)
occ++;
// Break the loop if given character has
// been occurred given no. of times
if (occ == count)
break;
}
// Print the string after the occurrence
// of given character given no. of times
if (i < str.length() - 1)
cout << str.substr(i + 1, str.length() - (i + 1));
// Otherwise string is empty
else
cout << "Empty string";
}
// Drivers code
int main()
{
string str = "geeks for geeks";
printString(str, 'e', 2);
return 0;