Implement strstr() Function in C Problem Statement

Given two strings A and B, find the index of the first occurrence of A in B. If A is not present in B, return -1.

Example:

Input:
A = "bc", B = "abcddbc"
Output:
1
Explanation:

The string A is found at index 1 and 5 (0-based index) in string B, but we return 1 as it is the first occurrence.

Input:

The first line contains an integer 'T' which denotes the number of test cases or queries to be run. Then, the T test cases follow. 
The first and only line of each test case contains two strings A and B, separated by a single space.

Output:

For each test case, print the index of the first occurrence of A in B. If string A is not present in string B then print -1.

Constraints:

  • 1 <= T <= 100
  • 1 <= |A|, |B| <= 5 * 10^4
  • Time limit: 1 second
Note:
You do not need to print anything. It has already been taken care of. Just implement the given function.
AnswerBot
4mo

Implement the strstr() function in C to find the index of the first occurrence of one string in another.

  • Iterate through the main string and check if the substring matches at each position.

  • Return the i...read more

Help your peers!
Select
Add answer anonymously...

Top Software Developer Interview Questions Asked at NetApp

Q. Define IP tables.
Q. Write a program to traverse a linked list
Q. In a knockout football tournament with n teams, how many matches need to be play...read more
Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits