473,624 Members | 2,249 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

string finding

Hello everyone,
I am eager to know about string functions, (user defined) . tell me
the technique of find a string in another string.

Nov 15 '05 #1
21 2043
Have you looked into strcmp and strncmp?

IF you want to write your own, u could use a for loop and keep on looking
for the first char and when that found, look for the second car.

John Dirk
Programming Consulant
http://www.programminghelp4u.com - Programming (Assignment/Project) Help

Nov 15 '05 #2
ne*****@gmail.c om wrote:

Hello everyone,
I am eager to know about string functions, (user defined) . tell me
the technique of find a string in another string.


#include <string.h>

char *str_str(const char *s1, const char *s2)
{
const int c = *s2++;

if (c != '\0') {
const size_t n = strlen(s2);

s1 = strchr(s1, c);
while (s1 != NULL && strncmp(s1 + 1, s2, n) != 0) {
s1 = strchr(s1 + 1, c);
}
}
return (char *)s1;
}

--
pete
Nov 15 '05 #3
proghelper wrote:
Have you looked into strcmp and strncmp?

IF you want to write your own, u could use a for loop and keep on looking


Please don't use abbreviations link "u". It makes it harder to read your
posts and makes you look stupid.

Please always quote enough of the original post to provide context since
the way Usenet works means that people might not, and indeed may
never, see the message you are replying to.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 15 '05 #4


ne*****@gmail.c om wrote:
Hello everyone,
I am eager to know about string functions, (user defined) . tell me
the technique of find a string in another string.


Try to rewrite strstr() function by yourself,it's very easy,isn't
it?And you can rewrite most of the functions in string.h,it's fun to do
that.

Nov 15 '05 #5
Use strstr routine defined in string.h

Nov 15 '05 #6


ne*****@gmail.c om wrote:
Hello everyone,
I am eager to know about string functions, (user defined) . tell me
the technique of find a string in another string.


/* The following is a public-domain implementation of the strstr()
function and the functions it calls in the <string.h> header. */

#include <string.h>

/* memcmp */
int (memcmp)(const void *s1, const void *s2, size_t n)
{
unsigned char *us1 = (unsigned char *) s1;
unsigned char *us2 = (unsigned char *) s2;
while (n-- != 0) {
if (*us1 != *us2)
return (*us1 < *us2) ? -1 : +1;
us1++;
us2++;
}
return 0;
}

/* strchr */
char *(strchr)(const char *s, int c)
{
/* Scan s for the character. When this loop is finished,
s will either point to the end of the string or the
character we were looking for. */
while (*s != '\0' && *s != c)
s++;
return ( (*s == c) ? (char *) s : NULL );
}

/* strlen */
size_t (strlen)(const char *s)
{
char *p = strchr(s, '\0');
return (p - s);
}

/* strstr */
char *(strstr)(const char *s1, const char *s2)
{
size_t s2len;
/* Check for the null s2 case. */
if (*s2 == '\0')
return (char *) s1;
s2len = strlen(s2);
for (; (s1 = strchr(s1, *s2)) != NULL; s1++)
if (memcmp(s1, s2, s2len) == 0)
return (char *) s1;
return NULL;
}

/* Hopefully this helps ... Gregory Pietsch */

Nov 15 '05 #7
ne*****@gmail.c om wrote:
Hello everyone,
I am eager to know about string functions, (user defined) . tell me
the technique of find a string in another string.


This is not strictly topical but I'll bite regardless. It depends on
how cute you want to get. You can simply use strstr, or roll your own
implementation if you think you're smarter than whoever implemented
your library (search comp.lang.c for strstr and timing for a thread
with sample code). If you want to be even fancier, you can use
Boyer-Moore or Knuth-Morris-Pratt algorithms. Thierry Lecroq in Rouen
has a very good online text (Exact String Matching Algorithms) with
sample code in C and animations in Java. Knock yourself out

Jan

Nov 15 '05 #8
ne*****@gmail.c om wrote:
# Hello everyone,
# I am eager to know about string functions, (user defined) . tell me
# the technique of find a string in another string.

Unless you have a reason not to use, strstr does this for you.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
If you plan to shoplift, let us know.
Thanks
Nov 15 '05 #9


ne*****@gmail.c om wrote:
Hello everyone,
I am eager to know about string functions, (user defined) . tell me
the technique of find a string in another string.

Why do you want user-defined ones? What's wrong the standard ones?


Brian

Nov 15 '05 #10

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

Similar topics

4
6945
by: lecichy | last post by:
Hello Heres the situation: I got a file with lines like: name:second_name:somenumber:otherinfo etc with different values between colons ( just like passwd file) What I want is to extract some part of it like all names or numbers from each line, simply text fom between e.g. second and third colon. And turn it
2
3133
by: Pawe³ | last post by:
Hello! I'm looking for efficient code or site where I can find code for finding one string in another string. String which I search should have "wild" characters like '?' for any one char and '*' for any string of characters. I'm looking for way to effective getting string from text file and then searching it like I write above. Thanks in advance for any helps, notices or sites
51
8254
by: Alan | last post by:
hi all, I want to define a constant length string, say 4 then in a function at some time, I want to set the string to a constant value, say a below is my code but it fails what is the correct code? many thx!
32
14830
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if ((someString.IndexOf("something1",0) >= 0) || ((someString.IndexOf("something2",0) >= 0) ||
0
1161
by: jrthor2 | last post by:
I have a .net application that is using microsofts locator service. I have this working, but when I select a user to locate from my contact list, it comes back with a map, but the pushpin has the users domain\username as the pin label. I would lik to show the person's display name instead. I was told I need to iterate through my array of locatable contacts, performing a string compare of pr.DomainAlias to LocatableContact.DomainAlias. ...
14
2159
by: Mr. B | last post by:
I want to return the name of the drawing file (DWG) from the end of a string. The string will be of varying lengths... as well as the drawing file name itself. I could do it the long way by getting the length of the string, and subtracting one character at a time until I hit the '\' character... then I'll know how much to subtract to get my file name. An example of the string is: "P:\Projects\2004\10-02-230 (ProjectName\600...
20
41281
by: anurag | last post by:
hey can anyone help me in writing a code in c (function) that prints all permutations of a string.please help
8
1423
by: Bas Nedermeijer | last post by:
Hello, what is a efficient solution to compare a string against a lot of possiblities? Currently i am having a lot of if (strcmp(checkme, "option1", strlen(checkme))) { value = OPTION1; }
10
430
by: Stu | last post by:
Can somebody please tell me the most effient away to parse the date YYYYMMDD from the following string. char *date_path = "/dira/dirb/dirc/dird/2006/12/04" Note: the number of directories before the date can vary. Thanks in advance for that respond
26
13782
by: Brad | last post by:
I'm writing a function to remove certain characters from strings. For example, I often get strings with commas... they look like this: "12,384" I'd like to take that string, remove the comma and return "12384" What is the most efficient, fastest way to approach this? Thanks,
0
8233
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
8675
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8619
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
8334
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,...
1
6108
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5561
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
4078
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
4173
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1784
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.