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

 
 
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
 
 
 
 
santosh
Guest
Posts: n/a
 
      03-22-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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/>

 
Reply With Quote
 
 
 
 
Martin Ambuhl
Guest
Posts: n/a
 
      03-22-2006
(E-Mail Removed) wrote:
> 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?
 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      03-22-2006
(E-Mail Removed) wrote:
> Ajay wrote:
> > Hi all,Could anybody tell me the most efficient method to find a substr
> > in a string.

>
> 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.

 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      03-22-2006
(E-Mail 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 <stdio.h>

> #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/gr.../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?

> getch();


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.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
 
Reply With Quote
 
Rod Pemberton
Guest
Posts: n/a
 
      03-22-2006

"Martin Ambuhl" <(E-Mail Removed)> wrote in message
news:h3gUf.8930$(E-Mail Removed) ink.net...
> (E-Mail 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?

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





 
Reply With Quote
 
Al Balmer
Guest
Posts: n/a
 
      03-22-2006
On Wed, 22 Mar 2006 14:06:49 -0500, "Rod Pemberton"
<(E-Mail Removed)> wrote:

>
>"Martin Ambuhl" <(E-Mail Removed)> wrote in message
>news:h3gUf.8930$(E-Mail Removed) link.net...
>> (E-Mail 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.

--
Al Balmer
Sun City, AZ
 
Reply With Quote
 
Rod Pemberton
Guest
Posts: n/a
 
      03-22-2006

"Al Balmer" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Wed, 22 Mar 2006 14:06:49 -0500, "Rod Pemberton"
> <(E-Mail Removed)> wrote:
>
> >
> >"Martin Ambuhl" <(E-Mail Removed)> wrote in message
> >news:h3gUf.8930$(E-Mail Removed) link.net...
> >> (E-Mail 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


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


There's some (not very well structured) information at:
http://en.wikipedia.org/wiki/String_searching_algorithm

A good place to ask would be comp.programming where they deal
with algorithm questions.

> By eye.


Come on, no horselling around please.

 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      03-23-2006
(E-Mail Removed) wrote:
> 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();
> }


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/>


 
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