Question

#include <stdio.h> /* Returns the number of occurrences of string t in s * * Example:...

#include <stdio.h>

/* Returns the number of occurrences of string t in s

*

* Example:

* substring_n("hello", "el") => 1

* substring_n("", "ab") => 0

* substring_n("utilities", "ti") => 2

*

*/

int substring_n(char *s, char *t)

{

return -1;

Homework Answers

Answer #1

ANS:

#include<stdio.h>
using namespace std;

int substring(char *s,char*t)
{
int j;
int count=0;
int subcount=0;
int index=0;
while(t[index]!='\0')
{
subcount++;
index++;
}
  
for(int i=0;s[i]!='\0';i++)
{
for( j=0;t[j]!='\0';j++)
{
if(s[i+j]!=t[j])
{
break;
}
}
  
if(index==j)
{
count++;
j=0;
}
}
return count;
}

int main()
{
char *str1=new char[50];
char *str2=new char[20];
printf(" Enter the first string:");
scanf("%c",str1);
  
printf(" Enter the first string:\n");
scanf("%c",str2);
  
printf("%d Count:",substring(str1,str2));
  
  
  
}

Comment down for any queries
Please give a thumbs up if you are satisfied with answer :)

Know the answer?
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
Similar Questions
#include <stdio.h> #define K   1024 /** (2pts) * Copy the string in src to dst. Only...
#include <stdio.h> #define K   1024 /** (2pts) * Copy the string in src to dst. Only copy as many characters (including the * null character) that are in src to dst. Points will be deducted for copying * too many or too few characters. */ void my_strcpy(char dst[], char src[]) { } int main(void) { char input[K+1], copy[K+1]; // Get a line of input (up to K number of bytes) into the 'input' string: // Do not modify any of...
2. Calculate number of times hello is printed. #include <stdio.h> #include <sys/types.h> int main() { fork();...
2. Calculate number of times hello is printed. #include <stdio.h> #include <sys/types.h> int main() { fork(); fork(); fork(); printf("hello\n"); return 0; }
C++ Write a function that returns a string of 1's and 0's. This needs to be...
C++ Write a function that returns a string of 1's and 0's. This needs to be the binary representation of a number. Do not use loops. Use only recursion. Do not change the function's parameters or add more parameters. Do not change the main program. #include #include string bin(int number) { //Code here } int main() {    cout << bin(43) << endl; // Should be 101011    return 0; }
Write a method called countChar which accepts a string parameter and a character parameter and returns...
Write a method called countChar which accepts a string parameter and a character parameter and returns an integer, that is: int countChar (String s, char c) This method should count the number of occurrences of the character c within the string s, and return the count to the caller. Also write a main method that tests countChar. All of the print statements and user interaction belong in the main method, not in countChar.
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 extern char **environ;    5 void output(char *a[], char...
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 extern char **environ;    5 void output(char *a[], char *b[]) { 6 int c = atoi(a[0]); 7 for (int i = 0; i < c && b[i]; ++i) { 8 printf("%s", b[i]+2); 9 } 10 } 11 12 void main(int argc, char *argv[]) { 13      14 switch (argc) { 15 case 1: 16 for (int i = 0; environ[i]; ++i) {    17 printf("%s\n", environ[i]); 18 } 19 break; 20 default: 21 output(argv +...
LANGUAGE C The codes below reads another .c or .txt file. Then, it will read the...
LANGUAGE C The codes below reads another .c or .txt file. Then, it will read the contents of every line and store the contents into a string array. Example: .c file that is to be read: #include <stdio.h> int main(){    printf("Hello World");    return ; } _______________(END OF THE FILE)_______________ Therefore, the code will read the contents above and store the contents in every line into an array, such that.... array[0] = "#include <stdio.h>\n" array[1] = "\n" array[2] ="int...
would someone kindly convert the following C code to java? thanks so much # include <stdio.h>...
would someone kindly convert the following C code to java? thanks so much # include <stdio.h> int main(void) { int changeOwed; int check; char invisibleChar; int count = 0; int numQ=0, numD=0, numN=0, numP=0; do{ printf("How much change is owed (in cents)?\n"); check = scanf("%d", &changeOwed); // returns 1 if the input is a number and returns 0 otherwise //Get's rid of any extra invisible characters if the input is a char/String do{ scanf("%c",&invisibleChar); }while(invisibleChar != '\n'); }while(check == 0...
Write a recursive method repeatNTimes(String s, int n) that accepts a String and an integer as...
Write a recursive method repeatNTimes(String s, int n) that accepts a String and an integer as two parameters and returns a string that is concatenated together n times. (For example, repeatNTimes ("hello", 3) returns "hellohellohello") Write a driver program that calls this method from the Main program. Program should ask for input and be stored in the string and should return n amount of times.
ruby code to count how many letter occurrences there are in a string. Example dna1 =...
ruby code to count how many letter occurrences there are in a string. Example dna1 = ('ATTGCC') output: {"A"=>1, "T"=>2, "G"=>1, "C"=>2}
Write a function to check whether string s1 is a substring of string s2. The function...
Write a function to check whether string s1 is a substring of string s2. The function returns the first index in s2 if there is a match. Otherwise, return -1. For example, the function should return 2 if s1 is "to" and s2 is "October". If s1 is "andy" and s2 is "candy", then the function should return 1. The function prototype is as follows: int indexOf(const char *s1, const char *s2) Write a program to test the function.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT