In: Computer Science
String Manipulator Write a program to manipulate strings.
(Visual Studio C++)
In this program take a whole paragraph with punctuations (up to 500
letters) either input from user, initialize or read from file and
provide following functionalities within a class:
a) Declare class Paragraph_Analysis
b) Member Function: SearchWord (to search for a
particular word)
c) Member Function: SearchLetter (to search for a
particular letter)
d) Member Function: WordCount (to count total
words)
e) Member Function: LetterCount (ONLY to count all
letters e.g ‘A’,’a’)
f) Member Function: FindReplaceWord (to find and
replace a word)
g) Member Function: FindReplaceLetter (to find and
replace a letter)
h) Member Function: Summary (to display summary of
frequency of each letter within the paragraph)
i) Of course, a menu is expected by user to know about
available functionalities of your Paragraph_Analysis
application.
Note: you can use access specifiers, return types and list of
parameters of your own choice.
I have written the C++ Program for the Above problem requirements and have shown few output, The code for program is :
#include<bits/stdc++.h>
using namespace std;
class Pragraph_Analysis
{
string paragraph; // Declaring the paragraph as a string variable
public:
Pragraph_Analysis(string & para)
{
paragraph = para;
}
bool SearchWord(string word)
{
int i=0,c=0;
while (paragraph[i]!='\0')
{
if (paragraph[i]==word[c] && word[c]!='\0' && paragraph[i]!=' ')
c++;
else
c=0;
i++;
}
if (c==word.length()) // if word found the return true else false
return true;
else
return false;
}
bool SearchLetter(char letter)
{
for(int i=0;i<paragraph.length();++i)
{
if(paragraph[i]==letter)
return true;
}
return false;
}
int WordCount(string word)
{
int count = 0;
int strLen = paragraph.length();
int wordLen = word.length();
int j;
for(int i=0; i <= strLen; i++)
{
for(j=0; j< wordLen; j++)
{
if(paragraph[i + j] != word[j])
{
break;
}
}
if(j == wordLen)
{
count++;
}
}
return count;
}
int LetterCount(char letter)
{
int count=0;
for(int i=0;i<paragraph.length();++i)
{
if(paragraph[i]==letter)
{
count++;
}
}
return count;
}
void ReplaceWord(string word,string newword)
{
int wordl=word.length();
for (int j = 0; j < paragraph.length(); j++)
{
string key = paragraph.substr(j, wordl), repl;
if (key == word) {
repl = newword;
for (int k = 0; k < wordl; k++)
{
paragraph[j+k] = repl[k];
}
}
}
}
void FindReplaceLetter(char letter,char newletter)
{
for(int i=0;i<paragraph.length();++i)
{
if(paragraph[i]==letter)
paragraph[i]=newletter;
}
}
void summary()
{
map<char,int> m;
for(int i=0;i<paragraph.length();++i)
{
m[paragraph[i]]++;
}
cout<<" Showing the count of every character in Paragraph in ascending order"<<endl;
for(auto c:m)
{
cout<<c.first<<" :"<<c.second<<endl;
}
}
};
int main ()
{
string text;
cout<<"Enter the Paragraph Text :\n";
getline(cin,text);
Pragraph_Analysis para(text); //created object of class
int choice=-1;
cout<<endl<<"Enter your choice from the Menu :\n";
cout<<"1. Search a word"<<endl;
cout<<"2. Search a letter"<<endl;
cout<<"3. Count the frequency of a word"<<endl;
cout<<"4. Count the frequency of a letter"<<endl;
cout<<"5. Find and Replace a word"<<endl;
cout<<"6. Find and Replace a letter"<<endl;
cout<<"7. To see Summary of paragraph"<<endl;
cin>>choice;
switch(choice)
{
case 1:
{
string word;
cout<<"Enter the word to be searched"<<endl;
cin>>word;
if(para.SearchWord(word))
cout<<"Found"<<endl;
else
cout<<"Not Found"<<endl;
}
break;
case 2:
{
char letter;
cout<<"Enter the letter to be searched"<<endl;
cin>>letter;
if(para.SearchLetter(letter))
cout<<"Found"<<endl;
else
cout<<"Not Found"<<endl;
}
break;
case 3: {string word;
cout<<"Enter the word to be count"<<endl;
cin>>word;
cout<<para.WordCount(word)<<endl;}
break;
case 4: {char letter;
cout<<"Enter the letter to be count"<<endl;
cin>>letter;
cout<<para.LetterCount(letter)<<endl;}
break;
case 5:{string word,newword;
cout<<"Enter the word to be replaced and new word"<<endl;
cin>>word;
cin>>newword;
para.ReplaceWord(word,newword);}
break;
case 6:{char letter,newletter;
cout<<"Enter the letter to be replaced and new letter"<<endl;
cin>>letter;
cin>>newletter;
para.FindReplaceLetter(letter,newletter);}
break;
case 7: para.summary();
break;
default:
cout<<"Wrong Choice"<<endl;
}
}
OUTPUT SAMPLES: