• C

check if a string is substring of another(Easy q)

I have a string named in a variable lets say X
I have another string variable called Y
All I need to do is check if X is a subset of Y

for example if X="S1P1"
and Y= "S1P1s"
then it should give me true when i check this

Easy question,rt..
Who is Participating?
ankuratvbConnect With a Mentor Commented:
#include <stdio.h>
#include <string.h>

int main(void)
   char *str1 = "Borland International", *str2 = "nation", *ptr;

   ptr = strstr(str1, str2);
   printf("The substring is: %s\n", ptr);
   return 0;

Also,if u want substring check without case sensitivity:
convert both strings to lower or upper case using strupr() or strlwr() and then do strstr()

use strstr() provided in string.h
For your e.g.

char *ptr=strstr(Y,X);
Become an IT Security Management Expert

In today’s fast-paced, digitally transformed world of business, the need to protect network data and ensure cloud privacy has never been greater. With a B.S. in Network Operations and Security, you can get the credentials it takes to become an IT security management expert.


This is homework, isn't it?
Avik DasguptaCommented:
Absolutely, this looks nothing but a homework. These type of assignments we used to get in our semesters and of course we had to implement them without using string libraries.

vikasgkuttyAuthor Commented:
how can this be homework, crazycomputers?
this is a very small check i wanted to do, in a project, tht I have been doing all sem,as Avik said

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.