Programming Fundamentals Assignment No 04

Here is the Programming Fundamentals Assignment No 04

Question:

Searching is an integral feature in majority of the application programs that we use. Write a program that obtains a paragraph from user as input, then asks the user to enter a phrase to be searched in the paragraph. Both the paragraph and the search-phrase should be passed to a function named Search( ) having the following signature:

int Search(string  text, string phrase)

This function is supposed to return the index/position of the phrase in the paragraph, otherwise -1 should be returned if the phrase can’t be located in the paragraph. Print the index/position of the phrase back in the main( ) function after calling the Search( ), or print appropriate message like “No such phrase is found” depending upon what has been returned.

Code:

Here is the Programming Fundamentals Assignment No 04

using namespace std;
int search(string,string,int);
int main()
{
string para,word;

cout<<"ENTER A PARAGRAPH: ";
getline(cin,para);

cout<<"ENTER A PHRASE: ";
getline(cin,word);

int size=word.length();
int index=search(para,word,size);

if (index!=-1)
{
    cout<<"THE PHRASE IS FOUND AT INDEX: "<<search(para,word,size);
}
else
{
    cout<<"THE PHRASE ENTERED IS NOT IN THE PARAGRAPH.";
}

return 0;

}

int search(string para,string word,int size)
{

int index=-1,left=0,i=0,v=0;

while (para[i]!='\0')
{
    string phrase;

    v=0;

    while (v<size)
    {
        phrase=phrase+para[left];
        left++;
        v++;
    }

    i++;
    left=left-1;

    if (phrase==word)
    {
        index=i-1;
        break;
    }
}

return index;

}

For more details about Huffman coding click here

For other assignments and quizzes click here 

Output:

Leave a Reply

Your email address will not be published. Required fields are marked *