Hi all,Could anybody tell me the most efficient method to find a substr
in a string. 29 51702
Ajay <aj***********@gmail.com> wrote: Hi all,Could anybody tell me the most efficient method to find a substr in a string.
By eye.
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
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.
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
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
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>
Ajay <ajay.sharm...@gmail.com> wrote: Hi all,Could anybody tell me the most efficient method to find a substr in a string.
By eye.
Be serious in usenet...
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
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();
}
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();
} 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.org/wiki/USENET>
<http://en.wikipedia.org/wiki/Netiquette>
<http://www.safalra.com/special/googlegroupsreply/> 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? 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.
"Martin Ambuhl" <ma*****@earthlink.net> wrote in message
news:h3*****************@newsread3.news.atl.earthl ink.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 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
On Wed, 22 Mar 2006 14:06:49 -0500, "Rod Pemberton"
<do*********@sorry.bitbuck.cmm> wrote: "Martin Ambuhl" <ma*****@earthlink.net> wrote in message news:h3*****************@newsread3.news.atl.earth link.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
"Al Balmer" <al******@att.net> wrote in message
news:lh********************************@4ax.com... On Wed, 22 Mar 2006 14:06:49 -0500, "Rod Pemberton" <do*********@sorry.bitbuck.cmm> wrote:
"Martin Ambuhl" <ma*****@earthlink.net> wrote in message news:h3*****************@newsread3.news.atl.earth link.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
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.programming where they deal
with algorithm questions.
By eye.
Come on, no horselling around please. 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.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/>
Al Balmer wrote: "Rod Pemberton"
.... snip ... You help the guy but then insult him?
Count the quote marks.
--
+-------------------+ .:\:\:/:/:.
| PLEASE DO NOT F :.:\:\:/:/:.:
| FEED THE TROLLS | :=.' - - '.=:
| | '=(\ 9 9 /)='
| Thank you, | ( (_) )
| Management | /`-vvv-'\
+-------------------+ / \
| | @@@ / /|,,,,,|\ \
| | @@@ /_// /^\ \\_\
@x@@x@ | | |/ WW( ( ) )WW
\||||/ | | \| __\,,\ /,,/__
\||/ | | | jgs (______Y______)
/\/\/\/\/\/\/\/\//\/\\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
================================================== ============
fix (vb.): 1. to paper over, obscure, hide from public view; 2.
to work around, in a way that produces unintended consequences
that are worse than the original problem. Usage: "Windows ME
fixes many of the shortcomings of Windows 98 SE". - Hutchison
Martin Ambuhl wrote:
[etc.] So, tell us: are you a troll or just a very, very bad programmer?
Son: "Dad, I'm a very, very bad programmer".
Dad: "No you're not, Son. Just stick with it."
Son: "Dad ... I've been thinking of making a post to CLC in the hopes
that it will make me a better programmer, but I'm a bit afraid."
Dad: "Afraid of what Son?"
Son: "Afraid that they'll call me a Troll."
Dad: "Don't worry Son. No one would ever do that."
Son: "Thanks Dad!"
....
Five agonizing days later
....
Dad: "So, Son, did you ever post to CLC?"
Son: "Yes Dad. I ... I ... I ... I did. :("
Dad: "What did they say, Son?"
Son" "That I'm a Troll."
Dad: "Sorry to hear that, Son."
....
One day later
....
Dad: "Did I mention to you that Mommy and I are getting a divorce?"
Son: "No Dad. Maybe next time I'll know better and choose C# for a
programming language."
--
jay
jaysome wrote: Martin Ambuhl wrote:
[etc.]
So, tell us: are you a troll or just a very, very bad programmer?
Son: "Dad, I'm a very, very bad programmer".
I think you're missing the point here. The code Martin criticised was
offered as /help/ to the OP. That's almost the same as throwing a brass
ring to a drowning man. Irresponsible at best, criminal at worst. In
c.l.c terms, attempt at trolling is probably somewhere between the two.
--
BR, Vladimir
"Vladimir S. Oka" <no****@btopenworld.com> writes: jaysome wrote: Martin Ambuhl wrote:
[etc.]
> So, tell us: are you a troll or just a very, very bad programmer?
Son: "Dad, I'm a very, very bad programmer".
I think you're missing the point here. The code Martin criticised was offered as /help/ to the OP. That's almost the same as throwing a brass ring to a drowning man. Irresponsible at best, criminal at worst. In c.l.c terms, attempt at trolling is probably somewhere between the two.
And the code contained so many fundamental errors, of kinds that have
been pointed out here again and again and again (void main(), gets(),
<conio.h>), as well as posting errors ("U" for "You", no context),
that it wasn't entirely unreasonable to assume that it might have been
deliberate. At the very least, the poster was trying to offer advice
without having read, or at least paid any attention to, this
newsgroup.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
jaysome wrote: Martin Ambuhl wrote: [etc.] So, tell us: are you a troll or just a very, very bad programmer?
Son: "Dad, I'm a very, very bad programmer". Dad: "No you're not, Son. Just stick with it." Son: "Dad ... I've been thinking of making a post to CLC in the hopes that it will make me a better programmer, but I'm a bit afraid." Dad: "Afraid of what Son?" Son: "Afraid that they'll call me a Troll."
<snip>
Although hinting that someone may be a troll based on a single posting
is a bit harsh, "binny's" code was shot full of unportable constructs
and terrible formatting, so Martin Ambuhl's reply is entirely
inappropriate.
On the other hand, hinting, (as you do in your post), that *all*
regulars in c.l.c are ready to label posters as trolls at the drop of a
hat, just hastens your progress into the killfile.
santosh wrote: jaysome wrote: Martin Ambuhl wrote: [etc.] So, tell us: are you a troll or just a very, very bad programmer?
Son: "Dad, I'm a very, very bad programmer". Dad: "No you're not, Son. Just stick with it." Son: "Dad ... I've been thinking of making a post to CLC in the hopes that it will make me a better programmer, but I'm a bit afraid." Dad: "Afraid of what Son?" Son: "Afraid that they'll call me a Troll." <snip>
Although hinting that someone may be a troll based on a single posting is a bit harsh, "binny's" code was shot full of unportable constructs and terrible formatting, so Martin Ambuhl's reply is entirely inappropriate.
^n't
santosh wrote: Although hinting that someone may be a troll based on a single posting is a bit harsh, "binny's" code was shot full of unportable constructs and terrible formatting, so Martin Ambuhl's reply is entirely inappropriate.
I admit being confused by this post. Everything before the final clause
suggests that it should have been "so Martin Ambuhl's reply is entirely
appropriate" or the weaker "so Martin Ambuhl's reply is not entirely
inappropriate." Could you explain binny's being "shot full of
unportable constructs and terrible formatting" (as well as outright
errors) makes my reply inappropriate?
Martin Ambuhl wrote: santosh wrote:
Although hinting that someone may be a troll based on a single posting is a bit harsh, "binny's" code was shot full of unportable constructs and terrible formatting, so Martin Ambuhl's reply is entirely inappropriate.
I admit being confused by this post. Everything before the final clause suggests that it should have been "so Martin Ambuhl's reply is entirely appropriate" or the weaker "so Martin Ambuhl's reply is not entirely inappropriate." Could you explain binny's being "shot full of unportable constructs and terrible formatting" (as well as outright errors) makes my reply inappropriate?
I assume that you can't see my follow-up post yet? It was a silly typo
that happened at the worst possible place! I meant:
....so Martin Ambuhl's reply isn't entirely inappropriate.
:)
jaysome wrote: Martin Ambuhl wrote:
[etc.]
So, tell us: are you a troll or just a very, very bad programmer?
Son: "Dad, I'm a very, very bad programmer".
[blither snipped]
You really aren't all that smart, are you?
Brian
int s1Index = 0,s2Index = 0,templastIndex = -1,lastIndex = -1;
BOOL possibility = FALSE,found =FALSE;
while(s1[s1Index]!='\0')
{
if(!possibility && s1[s1Index] == s2[0])
{
templastIndex = s1Index;
possibility = TRUE;
s1Index++; s2Index++;
}
else if((s1[s1Index]!='\0' && s2[s2Index]!='\0') && (s1[s1Index] == s2[s2Index]) )
{
s1Index++; s2Index++;
}
else if(s2[s2Index]=='\0')
{
found = TRUE;
lastIndex = templastIndex;
s1Index++; s2Index = 0;
possibility = FALSE;
}
else
{
s1Index++; s2Index = 0;
possibility = FALSE;
}
}//end while
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
by: Austin Varghese |
last post by:
How can i find a string from a file and hilight it?
somebody pls help me
|
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
|
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...
|
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...
|
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...
|
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...
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
| |