Using C write a function that will read in a pair of charact
Using C++ write a function that will read in a pair of characters representing the beginning and the end of ta range of characters respectively. Then read a group of words separated by a blank and print out each word whose first letter falls in the specified range.
Solution
#include<iostream>
#include<string>
//for function istringstreams
#include <sstream>
#define MAX 100
using namespace std;
void readFunction();
int main()
{
readFunction();
}
void readFunction()
{
char start, end;
string words[MAX];
string str;
int i = 0;
cout << \"Enter the beginning and the end of range of characters respectively\" << endl;
cin >> start;
cin >> end;
cout << \"Enter the words separated by space : \";
//to ignore newline character which will be read otherwise into str
cin.ignore(numeric_limits <streamsize> ::max(), \'\ \');
getline(cin, str);
istringstream iss(str);
//get each words into array of words
while (iss >> words[i++])
{
}
//print out words whose first letter falls in the specified range.
char ch;
for (int j = 0; j < i; j++)
{
ch = words[j][0];
if ( ch >= start && ch <= end)
{
cout << words[j] << endl;
}
}
}
-----------------------------------------------------------------------------------------
//output
Enter the beginning and the end of range of characters respectively
c f
Enter the words separated by space : cat swan fan lion dog
cat
fan
dog

