progrm to find a substring in a string

R

Robert Gamble

Ajay said:
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
 
S

santosh

Ajay said:
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.
 
S

somu

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
 
V

Vladimir S. Oka

somu said:
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.
 
S

santosh

somu said:
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>
 
B

binny.sam

Ajay said:
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
 
B

binny.sam

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();
}
 
B

binny.sam

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();
}
 
S

santosh

May this code help U.............
************************************
<snipped awful code>

What's the point in posting the same, unreadable, non-portable code in
three successive posts. Please quote the post to which you're replying.
You did it for your first post, (though the quoting was woefully
insufficient), but failed to do it for the subsequent reposts.

Please take a moment to read the material at the follwing URLs and heed
the advice given if you want to be considered seriously here.

<http://cfaj.freeshell.org/google/>
<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>
<http://en.wikipedia.org/wiki/USENET>
<http://en.wikipedia.org/wiki/Netiquette>
<http://www.safalra.com/special/googlegroupsreply/>
 
M

Martin Ambuhl

May this code help U.............

I don't know if the mysterious person U will appreciate your "help".
Some problems include:
#include<conio.h>
No such header in standard C.
void main()
Illegal return type for main.
clrscr();
No such function in standard C.
char s1[50],s2[20];
> int i,j,f=0;
Declarations after executable statement, illegal according the C89
standard, the one most common for extant C compilers.
printf("Enter main string : ");
A prompt without a '\n' or a following fflush(stdout). There is no
reason to think this prompt would ever be seen.
gets(s1);
One of the worst errors that can be made in a C program. Never use
gets() unless you really hate having a working computer.
[etc. ...]
So, tell us: are you a troll or just a very, very bad programmer?
 
S

santosh

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

This group deals only with the C language as defined by it's
international standards. Your code is quite non-standard and
implementation specific and hence is not topical here.
#include<stdio.h>
#include<conio.h>
#include<string.h>
int substring(char *,char *);

void main()

Ilegal form for main(). Use either int main(void) or int main(int argc,
char **argv).
{
clrscr();

Implementation specific function.
char s1[50],s2[20];
int i,j,f=0;
printf("Enter main string : ");
gets(s1);

Use of dangerous function gets(). Have you travelled in a time machine
from the 80s? Use fgets() as a standard alternative or ggets() by
CBFalconer as a non-standard but more robust variant.
printf("Enter substring to be searched in main string : ");
gets(s2);

Same as above.
if(substring(s1,s2)!=0)
printf("\nFound");
else
printf("\nNot found");
getch();

Non-standard function. getchar() is better.
}

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();
}

This function can be replaced by the standard strstr() function.
 
F

Flash Gordon

(e-mail address removed) wrote:

Please provide context. Google is not Usenet just an interface to it and
there is no guarantee that other people have seen the artical you are
replying to. See http://cfaj.freeshell.org/google/ for information on
how to provide context properly.

Also, please try to avoid multipl postings, although Google could well
be to blame for this.
May this code help U.............

Please avoid contractions like "U" for you, they make it much harder to
read your post and many more people will read it than write it.
************************************
#include<stdio.h>

Horizontal space if very cheap these days and makes it much easier to read.
#include said:
#include<conio.h>

conio.h is a non-standard header and we only deal with standard C here.
#include<string.h>
int substring(char *,char *);

void main()

main returns an int, not void. See http://c-faq.com/decl/main.html and
related questions.
{
clrscr();

Non standard function. See
http://dspace.dial.pipex.com/town/green/gfd34/art/software.html for
another possible result of using it.
char s1[50],s2[20];
int i,j,f=0;
printf("Enter main string : ");

This prompt might not be displayed before the the program waits for
input. You should use fflush(stdout); to flush the output to the display.
gets(s1);

NEVER use gets. Not ever. See http://c-faq.com/stdio/getsvsfgets.html
printf("Enter substring to be searched in main string : ");
gets(s2);
if(substring(s1,s2)!=0)
printf("\nFound");
else
printf("\nNot found");
getch();

getch is not a standard function. Why not use the standard getchar?

main should return an int, so so return one.
return 0;
}

int substring(char *s1,char *s2)

You are not modifying either string, so the following would be better
(also in your prototype)
int substring(const char *s1, const char *s2)
{
int f=0;
for(;*s1!='\0';)

If you are not providing initialisation or increment statement, why not
use a while? Also, why make one condition a seperate if?
while (*s1!='\0' && *s2!='\0')
{
if(*s2=='\0')
break;
for(;*s2!='\0';)

Again, using a for loop is stupid.
while (*s2!='\0)
{
if(*s1==*s2)
{
f=1;
s1++;
s2++;
}
else
{
f=0;
s1++;
break;
}
}
}
if(f==0)
return 0;
else
return 1;

Why not just return f?

Pointless since the code can never reach here.

I've not checked to see if the logic of your solution is sensible, there
were enough errors without reading that closely.
 
R

Rod Pemberton

Martin Ambuhl said:
May this code help U.............
[etc. ...]
So, tell us: are you a troll or just a very, very bad programmer?

You help the guy but then insult him?

It's like me saying: "I really liked Chapter 18 on DSP's that you wrote
(actually written by Jack Klein), but the rest of the book sucked. I want
refund."


Rod Pemberton
 
R

Rod Pemberton

Al Balmer said:
Martin Ambuhl said:
(e-mail address removed) wrote:
May this code help U.............
[etc. ...]
So, tell us: are you a troll or just a very, very bad programmer?

You help the guy but then insult him?
Count the quote marks.

Ajay posted a question.
binny.sam posted a solution.
Ambuhl helped binny.sam & insulted binny.sam.
I reply to Ambuhl's insult of binny.sam.
You tell me to count quote marks... (?)

Balmer, what the hell are you smoking out there in the desert? All posts
seem to be available to me and are quoted as stated...


Rod Pemberton
 
C

CBFalconer

Ajay said:
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();
}

Apart from the atrocious indentation and poor algorithm, use of the
non-std <conio.h>, and the non-std clrscr() and getch(), and the
failure to return exit status from main:

junk.c:7: warning: return type of `main' is not `int'
junk.c: In function `main':
junk.c:8: warning: implicit declaration of function `clrscr'
junk.c:9: warning: ISO C89 forbids mixed declarations and code
junk.c:19: warning: implicit declaration of function `getch'
junk.c:10: warning: unused variable `i'
junk.c:10: warning: unused variable `j'
junk.c:10: warning: unused variable `f'

Also, Mr U did not request any such. Ajay did ask for an efficient
method, without defining efficient.

There are basically two approaches, known as Knuth Morris Pratt and
Boyer Moore. KMP is especially attractive when input is from a
stream, and no backtracking is allowed. BM is potentially a good
deal more efficient. In practice the brute force algorithm, as
exemplified by Binnys code, will often be adequate.

Sedgewicks "Algorithms in C" discusses them all.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top