Solved

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

Posted on 2004-04-11
6
623 Views
Last Modified: 2012-06-27
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..
                         
0
Comment
Question by:vikasgkutty
6 Comments
 
LVL 9

Expert Comment

by:ankuratvb
Comment Utility
use strstr() provided in string.h
0
 
LVL 9

Accepted Solution

by:
ankuratvb earned 25 total points
Comment Utility
#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()

HTH
0
 
LVL 9

Expert Comment

by:ankuratvb
Comment Utility
For your e.g.

char *ptr=strstr(Y,X);
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 5

Expert Comment

by:crazycomputers
Comment Utility
vikasgkutty:

This is homework, isn't it?
0
 
LVL 2

Expert Comment

by:Avik77
Comment Utility
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.

Avik.
0
 

Author Comment

by:vikasgkutty
Comment Utility
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

0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now