In: Computer Science
USE C++ for the program and kindly keep it as simple as possible , use recursion and do not use loops please keep program simple and comment if possible
Suppose you have been given the task to design a text editor
which will take any
multiline text from user and then display the statistics like total
number of characters i.e.,
characters_count (excluding the white space and punctuations),
words_count, and
redundant_words_count. Create a structure named Text_Editor
having four type members
namely inserted_text (of type string), characters_count (of type
unsigned int),
words_count (of type unsigned int), and redundant_words_count (of
type unsigned int).
The structure should also have member functions namely Insert_Text(
) for obtaining the
multiline text from user and assigning to input_text
member-variable. End of text insertion
should be specified by ‘#’ character. Moreover, there should be a
separate member-function
of the created structure named Count_Ch( ), Count_Words( ),
and
Count_Redundant_Words( ) that should process the input text in
order to calculate and
print the relevant statistics. All these different tasks are to be
implemented using recursion
technique.
ANSWER :-
GIVEN THAT :-
#include <iostream>
#include <string>
#include <bits/stdc++.h>
using namespace std;
struct Text_Editor{
string inserted_text;
int characters_count;
int words_count;
int redundant_words_count;
void Insert_Text(string s){
inserted_text=s;
}
int i=0;
void Count_Ch(){//used to count characters
char char_array[inserted_text.length()+1];//Declaring a char
array
strcpy(char_array, inserted_text.c_str());//storing text in a char
array
char ch=char_array[i++];//Iterating through every single
character
int c=(int)ch;//Storing the ASCII value of character "ch" in
variable "c".
//How to get an ASCII value of a char
//for eg: if we want an ASCII value of 'a' then type convert that
char to int
//ASCII value of a=(int)'a'
/*In the below if statement we are getting the ASCII value of
'a','z','A','Z'
and checking the ASCII value of a char from char_array so that its
ASCII value
lies in the range of ASCII value of ('a','z') or ('A','Z') . in
that way we are
counting the number of characters in a string which are pure
alphabets
(ignoring spaces and other punctuation marks*/
if((c>=(int)'a'&&c<=(int)'z')||(c>=(int)'A'&&c<=(int)'Z')){
characters_count+=1;
}
if(i<=strlen(char_array)){
Count_Ch();
}
}
void Count_Words( ){
/*Here we are getting the total number of characters in a
string
irrespective of spaces and punctuation marks*/
words_count=inserted_text.length();
}
void Count_Redundant_Words( ){
/*This is simple we know the number of characters without
spaces
punctuation marks and also we know the total number of characters
in a string
irrespective of spaces and punctuation marks
so to find the number of redundant words
redundant words=total characters(including spaces and
punctuation)-characters(excluding spaces and punctuation)*/
redundant_words_count=words_count-characters_count;
}
};
int main()
{
Text_Editor t;
cout<<"Enter your text:";
string s;
getline(cin,s);
t.Insert_Text(s);
cout<<"Text entered is:"<<t.inserted_text;
t.Count_Ch();
cout<<"\nCharacters count:"<<t.characters_count;
t.Count_Words();
cout<<"\nWords count:"<<t.words_count;
t.Count_Redundant_Words();
cout<<"\nRedundant words
count:"<<t.redundant_words_count;
return 0;
}