Question

( no string methods or built-in string functions like strlen(), strcmp(), etc... the #include<strings.h> is not...

( no string methods or built-in string functions like strlen(), strcmp(), etc... the #include<strings.h> is not allowed)

Write a C program (no C+/C++) to enter a text. Then enter a pattern and count the number of times the pattern is repeated in the text. Your program will read in a string not a character at a time.

Output:

Enter string : She sells sea shells on the sea shore

Enter the pattern : sea

Pattern found 2 times

Print ‘Pattern not found’ if pattern is not found

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hey there, I hope you and your loved ones are safe and sound.

If my answer helps, please upvote.

Ans.

#include<bits/stdc++.h>
using namespace std;

int frequency(string &pat, string &txt)
{
   int M = pat.length();
   int N = txt.length();
   int fin = 0;

  
   for (int i = 0; i <= N - M; i++)
   {
      
       int t;
       for (t = 0; t < M; t++)
           if (txt[i+t] != pat[t])
               break;

      
       if (t == M)
       {
       fin++;
       t = 0;
       }
   }
   return fin;
}

int main()
{
string txt;
cout<<"Enter text: ";
cin>>txt;
cout<<endl;
string pat;
cout<<"Enter Pattern: ";
cin>>pat;
cout<<endl;
int result = 0;
result = frequency(pat, txt);
if(result==0){
cout<<"Pattern not found"<<endl;
}
else{
cout << "Pattern found "<<result<<" time(s)"<<endl;
}return 0;
}

#include<bits/stac++.h> using namespace std; int frequency (string spat, string stxt) int M = pat.length(); int N = txt.lengt

return fin; int main() string txt; cout<<Enter text: ; cin>>txt; cout<<endl; string pat; cout<<Enter Pattern: ; cin>>pat;

C:\Users\beris\Desktop\New folder\1q.exe, Enter text: banana Enter Pattern: nana Pattern found 1 time(s) execution time : 6

Add a comment
Know the answer?
Add Answer to:
( no string methods or built-in string functions like strlen(), strcmp(), etc... the #include<strings.h> is not...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C program write a program to enter a text. Then enter a pattern and count the number of times the pattern is repeated. Enter string: she sells sea shells near the sea. Enter pattern: sea pattern find...

    C program write a program to enter a text. Then enter a pattern and count the number of times the pattern is repeated. Enter string: she sells sea shells near the sea. Enter pattern: sea pattern find 2 times

  • you may not use any of the C-string library functions such as strlen(), strcpy(), strstr(), etc....

    you may not use any of the C-string library functions such as strlen(), strcpy(), strstr(), etc. You must write your own. You might consider writing helper functions to do tasks that many of these functions require, e.g. finding the last character of the string, and then use those helper functions where convenient. This function finds all instances of the char ‘target’ in the string and replaces them with ‘replacementChar’. It also returns the number of replacements that it makes. If...

  • Objectives: Use strings and string library functions. Write a program that asks the user to enter...

    Objectives: Use strings and string library functions. Write a program that asks the user to enter a string and output the string in all uppercase letters. The program should then display the number of white space characters in the string. You program should run continuously until the user enters an empty string. The program must use the following two functions: A function called count_spaces that counts the number of white spaces inside a string. int count_space(char str[]); which tell you...

  • Write in C. Simple Program (beginner) Assignment: Write a program Character Pointers and Functions. (like program...

    Write in C. Simple Program (beginner) Assignment: Write a program Character Pointers and Functions. (like program #5-5). Keyboard input to enter one character string. Using a single dimension array, populate the array with the character string, call a function using pointers to reverse order the character string, pass back to the main the output and total_count_of_characters. (maybe use a global variable for the total count). Print display the reversed char string and total_count. <END>

  • CODE THE FOLLOWING FUNCTIONS IN JAVASCRIPT (USE OF THE ABOVE MENTIONED IN BUILT FUNCTIONS IS NOT...

    CODE THE FOLLOWING FUNCTIONS IN JAVASCRIPT (USE OF THE ABOVE MENTIONED IN BUILT FUNCTIONS IS NOT ALLOWED) Write a function that accepts an array as argument. The function should loop through the array elements and accumulate the sum of ASCII value of each character in element and return the total. For example: function([‘A’, ‘bc’, 12]); // returns 361 which is the sum of 65 + 98 + 99 + 49 + 50 Write a function that accepts two arguments (a...

  • C++, program that use the built in string class, its constructors, some of its methods and...

    C++, program that use the built in string class, its constructors, some of its methods and operators. It reads an input file of English text, one word at a time. The file should consist of about 500 words. Keep only the words, removing any punctuation or other characters. .Store the words in a built-in STL container, such as vector or map.,Choose a list of about 10 words, for example, {she,, and, he, or, the}., Remove these words from the list.,,...

  • Write a Java program which takes a string as a user input. Create 2 functions. The...

    Write a Java program which takes a string as a user input. Create 2 functions. The first function expects a string as argument and returns void. It converts all upper case characters to lower case and converts all lower case characters to upper case. It then prints out the converted string to a console. The second function also expects a string as argument and returns void. It will find first charactor that is repeated exactly 2 times in the string....

  • In C Programming Language, write a program Character Pointers and Functions. Keyboard input to enter one...

    In C Programming Language, write a program Character Pointers and Functions. Keyboard input to enter one character string. Using a single dimension array, populate the array with the character string, call a function using pointers to reverse order the character string, pass back to the main the output and total_count_of_characters. (maybe use a global variable for the total count). Print display the reversed char string and total_count.

  • In C Programming Language, write a program Character Pointers and Functions. Keyboard input to enter one...

    In C Programming Language, write a program Character Pointers and Functions. Keyboard input to enter one character string. Using a single dimension array, populate the array with the character string, call a function using pointers to reverse order the character string, pass back to the main the output and total_count_of_characters. (maybe use a global variable for the total count). Print display the reversed char string and total_count.

  • Problem #1 Create a program that performs the following functions: Uses character arrays to read a...

    Problem #1 Create a program that performs the following functions: Uses character arrays to read a user's name from standard input Tells the user how many characters are in her name Displays the user's name in uppercase Create a program that uses the strstr() function to search the string "When the going gets tough, the tough stay put! for the following occurrences (display each occurrence found to standard output): "Going" "tou" "ay put!" Build a program that uses an array...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT