In: Computer Science
For our current programming assignment I need to create a CleanWord method that reads in one variable string at a time and returns a cleaned word without any non letters. The instructions given to us are listed below:
“Clean word” method:
This is to be implemented as a separate method, so it can be called for either a dictionary word or book word.
The function will remove any non-letters, except an apostrophe (‘) and numbers from the word.
All letters will changed to lower case.
It will then return the updated word. The word could now be blank. The calling method will need to deal with this correctly.
I would appreciate the help!
Thanks for the question.
Below is the code you will be needing Let me know if you have
any doubts or if you need anything to change.
Thank You !!
===========================================================================
#include<iostream>
#include<string>
#include<sstream>
using namespace std;
string cleanWord(string &word){
stringstream ss;
char letter;
for(int i=0; i<word.size(); i++){
letter = word.at(i);
if('a'<=letter &&
letter<='z'){
ss<<letter;
}else if('A'<=letter &&
letter<='Z'){
letter = letter
- ('A'-'a');
ss<<letter;
}else if(letter=='\''){
ss<<letter;
}
}
string cleanedWord;
ss>>cleanedWord;
return cleanedWord;
}
int main(){
string word =
"'cleaned#%@#^&362464'WORD3523626";
cout<<cleanWord(word)<<endl;
}
============================================================================