Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > progrm to find a substring in a string

Reply
Thread Tools

progrm to find a substring in a string

 
 
Ajay
Guest
Posts: n/a
 
      03-22-2006
Hi all,Could anybody tell me the most efficient method to find a substr
in a string.

 
Reply With Quote
 
 
 
 
russell kym horsell
Guest
Posts: n/a
 
      03-22-2006
Ajay <(E-Mail Removed)> wrote:
> Hi all,Could anybody tell me the most efficient method to find a substr
> in a string.


By eye.
 
Reply With Quote
 
 
 
 
Robert Gamble
Guest
Posts: n/a
 
      03-22-2006
Ajay wrote:
> Hi all,Could anybody tell me the most efficient method to find a substr
> in a string.


That depends on what you mean by substring. If you are looking to find
the first occurence of a particular string within another string, check
out the standard function strstr.
If you are looking for a replacement for the "substring" function found
in certain other languages that returns a new string provided a given
string, offset, and length then see my post from 3/18/2006 in the
thread "Sub strings".

Robert Gamble

 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      03-22-2006
Ajay wrote:
> Hi all,Could anybody tell me the most efficient method to find a substr
> in a string.


You need to define your question better. I'm guessing that 'substr' is
a sub-string...

I don't know how efficient it is, but a portable way is to use the
standard strstr() function. Look it up in a good standard library
reference.

 
Reply With Quote
 
somu
Guest
Posts: n/a
 
      03-22-2006
hi ajay
take the two string in the form of array
compare the second with the first
through loop
match both
nu will get the matching string
that can b later taken out

 
Reply With Quote
 
Vladimir S. Oka
Guest
Posts: n/a
 
      03-22-2006

somu wrote:
> hi ajay
> take the two string in the form of array
> compare the second with the first
> through loop
> match both
> nu will get the matching string
> that can b later taken out


Without any context it's impossible to see what you're talking about.
Your childish SMS-speak is not exactly helping either. On top of all
that, your advice is so poorly presented that I don't think it can be
used as a basis for any implementation.

Read these:

<http://cfaj.freeshell.org/google/>
<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>

if you want to be taken seriously and understood around here.

--
BR, Vladimir

 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      03-22-2006
somu wrote:
> hi ajay
> take the two string in the form of array
> compare the second with the first
> through loop
> match both
> nu will get the matching string
> that can b later taken out


You're apparently addressing the OP while replying to my post and
failing to include any context whatsoever, even after being requested
to do so previously. Please read the content at the following URLs and
heed the advice if you don't want to be ignored or plonked by most of
the regulars in this group.

<http://cfaj.freeshell.org/google/>
<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>
<http://en.wikipedia.org/wiki/Netiquette>
<http://en.wikipedia.org/wiki/USENET>

 
Reply With Quote
 
code break
Guest
Posts: n/a
 
      03-22-2006
Ajay <(E-Mail Removed)> wrote:
> Hi all,Could anybody tell me the most efficient method to find a substr
> in a string.


By eye.

Be serious in usenet...

 
Reply With Quote
 
binny.sam@gmail.com
Guest
Posts: n/a
 
      03-22-2006

Ajay wrote:
> Hi all,Could anybody tell me the most efficient method to find a substr
> in a string.



Given Code can help U................


************************************************** ******************************

#include<stdio.h>
#include<conio.h>
#include<string.h>
int substring(char *,char *);

void main()
{
clrscr();
char s1[50],s2[20];
int i,j,f=0;
printf("Enter main string : ");
gets(s1);
printf("Enter substring to be searched in main string : ");
gets(s2);
if(substring(s1,s2)!=0)
printf("\nFound");
else
printf("\nNot found");
getch();
}

int substring(char *s1,char *s2)
{
int f=0;
for(;*s1!='\0'
{
if(*s2=='\0')
break;
for(;*s2!='\0'
{
if(*s1==*s2)
{
f=1;
s1++;
s2++;
}
else
{
f=0;
s1++;
break;
}
}
}
if(f==0)
return 0;
else
return 1;
getch();
}

************************************************** **************************************
Author BINNY

 
Reply With Quote
 
binny.sam@gmail.com
Guest
Posts: n/a
 
      03-22-2006
May this code help U.............
************************************
#include<stdio.h>
#include<conio.h>
#include<string.h>
int substring(char *,char *);

void main()
{
clrscr();
char s1[50],s2[20];
int i,j,f=0;
printf("Enter main string : ");
gets(s1);
printf("Enter substring to be searched in main string : ");
gets(s2);
if(substring(s1,s2)!=0)
printf("\nFound");
else
printf("\nNot found");
getch();
}

int substring(char *s1,char *s2)
{
int f=0;
for(;*s1!='\0'
{
if(*s2=='\0')
break;
for(;*s2!='\0'
{
if(*s1==*s2)
{
f=1;
s1++;
s2++;
}
else
{
f=0;
s1++;
break;
}
}
}
if(f==0)
return 0;
else
return 1;
getch();
}

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
trying to find a substring in a string barnacle.steve@gmail.com Python 1 06-21-2008 11:11 PM
progrm for converting integers to roman numerals using files in clanguage kotlakirankumar@gmail.com C Programming 26 01-28-2008 09:55 PM
RegEx search for a substring within a substring colinhumber@gmail.com Perl Misc 3 08-03-2005 04:29 PM
find if there is a given substring inside a string juli ASP .Net 3 12-06-2004 11:52 AM
Find location of substring in string? fishfry Perl Misc 3 09-24-2004 06:13 AM



Advertisments