Function with no return values Write a C program for 6 II Di

Function with no return values:
Write a C++ program for

6. II (Display characters) Write a function that prints characters using the following header: void printChars (char ch1, char ch2, int numberPerLine) This function prints the characters between chl and ch2 with the specified number of characters per line. Write a test program that prints 10 characters per line from \'1\' and \'Z\'. Characters are separated by exactly one space.

Solution

Program Code:

#include <iostream>
using namespace std;

void printChars(char ch1, char ch2, int numberPerLine)
{
   char letter1 = ch1;
   char letter2 = ch2+1; // incrementing to the next letter
   int counter = 1; // counter for counting the characters printed
   while(letter1 != letter2) // checking till ch1 is not equal to ch2
   {
       if(counter<numberPerLine)
       {
           cout<<letter1<<\" \"; // printing with one space when the counter is less than numberPerLine
       }
       else
       {
           cout<<letter1<<\"\ \"; //printing a new line when the numberPerLine is reached.
           counter = 0;
       }
       letter1 = letter1 + 1; // incrementing the letter with one. This moves to the next character in the alphabetical order.
       counter++;
   }
   cout<<\"\ \ \";
}

int main() {
   // All uppercase letters using 6 characters perline
   cout<<\"All letters in lowercase\ \ \";
   printChars(\'a\', \'z\', 6);
   // All lowercase letters using 8 characters per line
   cout<<\"All letters in uppercase\ \ \";
   printChars(\'A\', \'Z\', 8);
   // All charcaters between \'l\' and \'Z\' using 10 characters per line
   cout<<\"All letters between \'l\' and \'z\'\ \ \";
   printChars(\'l\', \'z\', 10);
   return 0;
}

OUTPUT:

Function with no return values: Write a C++ program for 6. II (Display characters) Write a function that prints characters using the following header: void prin

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site