473,839 Members | 1,460 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

progrm to find a substring in a string

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

Mar 22 '06
29 51759
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();
}

Mar 22 '06 #11
bi*******@gmail .com 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.or g/wiki/USENET>
<http://en.wikipedia.or g/wiki/Netiquette>
<http://www.safalra.com/special/googlegroupsrep ly/>

Mar 22 '06 #12
bi*******@gmail .com 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?
Mar 22 '06 #13
bi*******@gmail .com 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.

Mar 22 '06 #14

"Martin Ambuhl" <ma*****@earthl ink.net> wrote in message
news:h3******** *********@newsr ead3.news.atl.e arthlink.net...
bi*******@gmail .com 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

Mar 22 '06 #15
bi*******@gmail .com 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
Mar 22 '06 #16
On Wed, 22 Mar 2006 14:06:49 -0500, "Rod Pemberton"
<do*********@so rry.bitbuck.cmm > wrote:

"Martin Ambuhl" <ma*****@earthl ink.net> wrote in message
news:h3******* **********@news read3.news.atl. earthlink.net.. .
bi*******@gmail .com 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
Mar 22 '06 #17

"Al Balmer" <al******@att.n et> wrote in message
news:lh******** *************** *********@4ax.c om...
On Wed, 22 Mar 2006 14:06:49 -0500, "Rod Pemberton"
<do*********@so rry.bitbuck.cmm > wrote:

"Martin Ambuhl" <ma*****@earthl ink.net> wrote in message
news:h3******* **********@news read3.news.atl. earthlink.net.. .
bi*******@gmail .com 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
Mar 22 '06 #18
russell kym horsell wrote:
Ajay <aj***********@ gmail.com> 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.programmin g where they deal
with algorithm questions.
By eye.


Come on, no horselling around please.

Mar 22 '06 #19
bi*******@gmail .com 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.c om, 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/googlegroupsrep ly/>
Mar 23 '06 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
8963
by: Jozef Jarosciak | last post by:
Quickest way to find the string in 1 dimensional string array! I have a queue 1 dimensional array of strings called 'queue' and I need a fast way to search it. Once there is match, I don't need to search any longer. Currently I am using this code. But I think it's too slow, because it runs through whole dimension. I know this is trivial question, but is there any way to stop this loop, or better way to search? I mean - FASTER?
2
1827
by: Robin Tucker | last post by:
Hiya, Regular expressions always do my gnads in - can anyone cook up a reg expression to find all string literals in my code? I would like to put them into a resource file for future translation :) Thanks,
2
1763
by: Gaijinco | last post by:
I was trying a function that read an string (which previously had attached two special characters & for the beginning of the word and # for the end of the word) and if it was "<word>ize" it changed into "change into <word>". I did this function: bool Ize(string& s) { bool is=false; string eval="ize"; int end=s.find('#');
3
1703
by: Austin Varghese | last post by:
How can i find a string from a file and hilight it? somebody pls help me
1
2472
by: sai14 | last post by:
Hi all, i would like to know if it is possible find particular string in a text file using vc++? please do let me know how to write the syntax Thanks
2
1311
by: Pradeep Arkasali | last post by:
directory is /v_dialer in that i have to find the string v_dialer in all the files as well as subdirectories, here for some of the files i don't have ownership,how can i use ownership in this context. reply ASAP drwxrwxr-x 11 ccvob01 ccgrp01 236 Aug 13 2005 3rdparty -r-xr-xr-x 1 ccvob01 ccgrp01 4797 Mar 14 13:56 Make.system -r-xr-xr-x 1 ccvob01 ccgrp01 8488 Feb 17 2001 Make.targets -r-xr-xr-x 1 ccvob01 ...
5
8356
by: neeludhiman | last post by:
Hi All, Can someone please help me with the code in C / C++ to find a string in an input text file and replace it with another string in output text file. The catch is that white spaces in the input file should be retained in output file. And, if possible we should be able to replace multiple search strings with multiple replace strings. e.g; srch1, srch2, srch3 in in.txt should be replaced by rplc1, rplc2, rplc3 in out.txt Thanks.
2
2343
by: powerfulperl | last post by:
I want to locate a string 'Local=IN' from a file and I am sure that this string is located within 100 lines(assumption) from the beginning of the file out of 5000 lines. The 100th line start with the word '--Begin'. So in order to locate the string from only 100 line, I think copying all the file content into any array is not a good idea. I want the grep command to exist in the script. I have the following code. I need this to be modified...
3
1405
by: Rob S | last post by:
Hi, I am not sure where I am going wrong with this code. It seems to work fine for a small text file but when I use files larger than 100MB it does not give me an accurate count. The program is pretty simple. It should return the number of occurences of a substring from within a text file. I look for a match to the first character of the substring and if found then my code should test to see if the whole substring exists. I have run...
0
9854
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
10584
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10645
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10290
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7015
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5681
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5865
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4482
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4063
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.