In: Computer Science
Please, write code in c++. Using iostream and cstring library
Write a function that will find and return most recent word in
the given text.
The prototype of the function have to be the following void
mostRecent(char *text,char *word)
In char *word your function have to return the most recent word
that occurce in the text.
Your program have to be not case-sensitive(ignore case -
"Can" and "CAN" are the same
words)
Also note than WORD is sequence of letters sepereated by
whitespace.
Note. The program have to use pointer.
Input:
First line contains one line that is not longer than 1000 symbols
with whitespaces.
Output: The most recent word in UPPER case.
example:
Input:
Can you can the can with can?
output:
CAN
I have implemented the "void mostRecent(char *text,char *word) " per the given description.
Please find the following Code Screenshot, Output, and Code.
ANY CLARIFICATIONS REQUIRED LEAVE A COMMENT
1.CODE SCREENSHOT :
2.OUTPUT :
3.CODE :
#include<iostream>
#include<cstring>
using namespace std;
void mostRecent(char *text,char *word){
//variables to iterate
int i,j=0,t=0;
//loop until we reach end of the string
for(i=0;text[i]!='\0';i++){
//if the symbol is an uppercase character then
//we copy that symbol
if(text[i]>=65&&text[i]<=90){
word[j++]=text[i];
t=j;
}
else if(text[i]>=97&&text[i]<=122)
{
//if the symbol is an lowercase character then
//we copy convert that symbol to upper case
word[j++]=(char)((int)text[i]-32);
t=j;
}//if the symbol is not a alphabet then we skip that and begin new word
else
j=0;
}
//Place a null character at the end of string
word[t]='\0';
}
int main(){
//to store the text
char text[1000];
//to store the word
char word[1000];
//to read the string with spaces
cout<<"Enter a String : ";
cin.getline(text,1000);
//determine the most recent word
mostRecent(text,word);
//display the result
cout<<word;
}