Question

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

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

//C program

#include <stdio.h>
#include <string.h>
  
int find(char* pattern, char* master)
{ int masterindex;
int patternindex;
  
//find the length of pattern string and master string
int M = strlen(pattern);
int N = strlen(master);
   int count=0;
/* A loop to slide pat[] one by one */
for ( masterindex = 0; masterindex <= N - M; masterindex++) {


/* For current masterindex, check for pattern match */
for (patternindex = 0; patternindex < M; patternindex++)
if (master[masterindex + patternindex] != pattern[patternindex])
break;
  
if (patternindex == M)count++;
}
return count;
}
  
/* Main program to test above function */
int main()
{
char text[100] ;
printf("Enter String : ");
scanf("%[^\n]%*c",text);

char pattern[20];;
   printf("Enter pattern : ") ;
   scanf("%[^\n]%*c",pattern);
  
   //calling pattern finding function
   printf("pattern find %d times\n",find(pattern, text) );
  
return 0;
}

//sample output

C:Users IshuManish\ Documents Pattern.exe Enter String she sells sea shells near the sea Enter pattern pattern find sea 2 tim

Add a comment
Know the answer?
Add Answer to:
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...
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
  • ( 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...

  • This is the text for the famous “sea shells” tongue twister: sea_shells <- c( "She", "sells",...

    This is the text for the famous “sea shells” tongue twister: sea_shells <- c( "She", "sells", "sea", "shells", "by", "the", "seashore", "The", "shells", "she", "sells", "are", "surely", "seashells", "So", "if", "she", "sells", "shells", "on", "the", "seashore", "I'm", "sure", "she", "sells", "seashore", "shells" ) Use the nchar() function to calculate the number of letters in each word. Now loop over possible word lengths, displaying a message about which words have that length. For example, at length six, you should state that...

  • Python Write a program which reads in a string and an signifying the number times the...

    Python Write a program which reads in a string and an signifying the number times the string should be repeated. The program should then output the string N times. integer N, You can assume all input will be valid. Example: Enter a string: hello How many times to repeat? 5 hello hello hello hello hello Example: T Enter a string: goodbye Ty How many times to repeat? 32 goodbye goodbye [... goodbye 30 more times]

  • Write a program that asks the user to enter a string and then asks the user...

    Write a program that asks the user to enter a string and then asks the user to enter a character. The program should count and display the number of times that the specified character appears in the string. python programming

  • Write a case-insensitive pattern searcher that prompts the user for a file name and a pattern,...

    Write a case-insensitive pattern searcher that prompts the user for a file name and a pattern, and the program reports on the number of times the pattern occurs within the file. If you were searching for the pattern "ab" occurrences of "ab", "AB", "aB", "Ab"  would all contribute to the count. Hint: The argument to the string function count could be a multi-character string. Shown here are three sample runs of the program: >>> main() Enter file name: AllYouNeedIsLove.txt Enter string...

  • Write a program that prompts the user to enter the number of milliseconds and converts the milliseconds to a string hours:minutes:seconds

    Problem 1.Write a program that prompts the user to enter the number of milliseconds and converts the milliseconds to a string hours:minutes:seconds. The program should use the convertMillismethod with the following header:public static String convertMillis(long millis)For example, convertMillis(5500) returns the string 0:0:5, convertMillis(100000) returns the string 0:1:40, andconvertMillis(555550000) returns the string154:19:10.Problem 2. (Count occurrence of numbers)Write a program that reads integers between 1 and 100 and counts the occurrence of each (you should store the numbers in an array). Output...

  • Write a C program where user enter a string input, tokenizer the string using space character....

    Write a C program where user enter a string input, tokenizer the string using space character. Calculate the lengh of string. Find word start. Find word end. Count words.

  • ‘C’ programming language question Write a MENU DRIVEN program to A) Count the number of vowels...

    ‘C’ programming language question Write a MENU DRIVEN program to A) Count the number of vowels in the string B) Count the number of consonants in the string C) Convert the string to uppercase D) Convert the string to lowercase E) Display the current string X) Exit the program The program should start with a user prompt to enter a string, and let them type it in. Then the menu would be displayed. User may enter option in small case...

  • MIPS Assembly Language * Write a simple program to count the number of non overlapping repetitions...

    MIPS Assembly Language * Write a simple program to count the number of non overlapping repetitions of a character pattern in a character string. For example " the pattern "aa" appears twice in the in the string "aabbaaa". Provide a user interface to read the character pattern and the string.

  • Using C++, and <iostream> if possible, ​ Write a program that can compress a text file...

    Using C++, and <iostream> if possible, ​ Write a program that can compress a text file consisting of English language alphabet A-zl using run-length encoding scheme. (Weight 50%) Run-Length Encoding The key idea is to replace the repeating character by itself and the number of times that it has been repeated. Consider the following string HELLO WORLD This can be re-written as HEL20 WORLD Notice that since LL was repeated, it was repeated by L2. 2 here indicates that letter...

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