Sign Up

Have an account? Sign In Now

Sign In

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

You must login to add post.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

mcqoptions.com

mcqoptions.com Logo mcqoptions.com Logo

mcqoptions.com Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 344122
Next
In Process
Vineeta Ghanshyam Jhaveri
Vineeta Ghanshyam Jhaveri
Asked: 3 years ago2022-11-04T05:35:24+05:30 2022-11-04T05:35:24+05:30In: Pointers

Write a function substr() that will scan a string for the occurrence of a give substring. the prototype of the function would be:

char *substr(char *string1,char string2);

The function should return a pointer to the element in string1 where strig2 begins. If string2 doesn’t occur in string1 then substr() should return a NULL.

For example, if string1 is “Ena Meena Deeka”, and string2 is “Meena” the substr() should return address of ‘M’ in string1. 

Write main() function also that uses the function substr().

Write a function substr() that will scan a string for the occurrence of a give substring. the prototype of the function would be:

char *substr(char *string1,char string2);

The function should return a pointer to the element in string1 where strig2 begins. If string2 doesn’t occur in string1 then substr() should return a NULL.

For example, if string1 is “Ena Meena Deeka”, and string2 is “Meena” the substr() should return address of ‘M’ in string1. 

Write main() function also that uses the function substr().

Waves
  • 0
  • 11
  • 6
  • 0
  • 0
    • Report
  • Share
    • Share on Facebook
    • Share on Twitter
    • Share on LinkedIn
    • Share on WhatsApp
Leave an answer

Leave an answer
Cancel reply

Browse

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. eac99
    2022-11-09T23:21:52+05:30Added an answer about 3 years ago

    #include <iostream.h>

    #include<stdio.h>

    char *get_substr(char *sub, char *str);

    int main()

    {

     char *substr,*str;

     cout<<“Enter the string :”;

     gets(str);

     cout<<“\nEnter the substring :”;

     gets(substr);

     substr = get_substr(substr,str);

     if (substr!=’\0′)

     cout << “substring found: ” <<substr;

     else

     cout<< “substring not found”;

     return 0;

    }

    char *get_substr(char *sub, char *str) // Return pointer to substring or

    null if not found.

    {

     int t;

    char *p, *p2, *start;

    for(t=0; str[t]!=’\0′; t++)

    {

    p = &str[t];

    start = p;

    p2 = sub; 

     while(*p2!=’\0′ && *p2==*p) // check for substring

    {

    p++;

    p2++;

    }

    /* If at end of p2 (i.e., substring), then a match has been found. */ if(!*p2)

    return start; // return pointer to beginning of substring 

    }

    return 0;

    }

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 500k
  • Answers 393k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Aditi Dugal

    How to approach applying for a job at a company ...

    • 7 Answers
  • Raghavan Prasad Hayer

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Ankita Dinesh Biswas

    What is a programmer’s life like?

    • 5 Answers
  • 47e0c
    47e0c added an answer Correct Answer - Increasing the yield of animals and improving… November 12, 2022 at 9:56 am
  • b6699
    b6699 added an answer Sender\'s addressDateReceivers name and addressSubjectContentYours faithfullyName November 12, 2022 at 9:56 am
  • 10eb8
    10eb8 added an answer Any uncertinity in measurment is known as errorDifference in true… November 12, 2022 at 9:56 am

Related Questions

  • C++ lets you pass a structure by value ...

    • 1 Answer
  • Write a function having this prototype:

    • 1 Answer
  • Imagine a publishing company that markets both ...

    • 1 Answer
  • What is the advantage of passing arguments ...

    • 1 Answer
  • Suppose 7 names are stored in a ...

    • 1 Answer

Top Members

Trending Tags

Class 11 Parabola Polity Polynomials Probability Projectile Protists Quadrilaterals Rario Reasoning Sampling Social Solutions Spectroscopy Switchgear Thermodynamic Tourism Transients Upsc Wbjee

Explore

  • Home
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Tags
  • Badges
  • Users

Footer

mcqoptions.com

About

MCQOptions.com

Here are the top interview questions, example answers, tips for giving the best response.

About Us

  • About Us
  • Contact Us

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Follow

© 2022 MCQOptions. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.