Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 131,513 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,858 people online right now. Registration is fast and FREE... Join Now!




searching a string

 
Reply to this topicStart new topic

searching a string

Artaniz
post 5 Dec, 2005 - 11:13 AM
Post #1


D.I.C Head

**
Joined: 11 Apr, 2005
Posts: 127


My Contributions


i need to search a string then return how far is the char is.

i know how to search for a single char, but the program requires that we search for a whole word or more than 1 letter.

he is what it is supposed to look like.

Enter state: Tennesse
Enter term: es

Tennesse, es - 4





my problem is that if i search for 'es' then it finds the first instance of 'e' but not the whole term 'es'

heres my code it only loops once so far.


CODE
char str[] = "tennessee";
 char par[] = "es";
 int i;
 i = strcspn (str,par);
 cout <<"The differnce in "<< str <<" between " << par << " is " << i << endl;
User is offlineProfile CardPM

Go to the top of the page


Nova Dragoon
post 5 Dec, 2005 - 11:16 AM
Post #2


The Innocent Shall Suffer, Big Time

Group Icon
Joined: 16 Aug, 2001
Posts: 6,124



Thanked 4 times

Dream Kudos: 515

Expert In: Python, Linux

My Contributions


look for examples of the naive-string matcher algorithm, or the KMP-Matcher algoritm

the naive uses 2 nested for-loops

the outer keeps track of index of the string you are searching in
the inner keeps track of the index of the string you are searching for
User is offlineProfile CardPM

Go to the top of the page

Artaniz
post 5 Dec, 2005 - 11:21 AM
Post #3


D.I.C Head

**
Joined: 11 Apr, 2005
Posts: 127


My Contributions


? i have never hear do that?

what is it, where is it...

heres the main prob. if i enter an 'e' to search within 'Tennessee', it find the first 'e' which is correct. but if i enter 'es' then it only searches for the 'e' and forgets the 's'
User is offlineProfile CardPM

Go to the top of the page

Nova Dragoon
post 5 Dec, 2005 - 11:23 AM
Post #4


The Innocent Shall Suffer, Big Time

Group Icon
Joined: 16 Aug, 2001
Posts: 6,124



Thanked 4 times

Dream Kudos: 515

Expert In: Python, Linux

My Contributions


google search
"naive string matcher"

Here is one of the first things tha pops up
User is offlineProfile CardPM

Go to the top of the page

Artaniz
post 5 Dec, 2005 - 11:29 AM
Post #5


D.I.C Head

**
Joined: 11 Apr, 2005
Posts: 127


My Contributions


i found something but of course it is way to complex for me

CODE

/* Naive Strint Matching Algorithm
   strMatch(T,P)
   - match whether P is substring of T
   - return the starting index of the first occurence of P in T
*/

#include <cstdio>
int strMatch(char *T,char *P)
{
   int i,j,n,m,found;
   n = strlen(T);
   m = strlen(P);
             
   for (i=0; i<=n-m; i++)
   {
       found = 1;
       for (j=0; j<m; j++)
          if (P[j] != T[i+j])
          {
              found = 0;
              break;
          }
       if (found) return i;
   }
   return -1;
}


i dont understand this can you help me anyalize it or explian it to me

This post has been edited by Artaniz: 5 Dec, 2005 - 11:30 AM
User is offlineProfile CardPM

Go to the top of the page

Nova Dragoon
post 5 Dec, 2005 - 02:18 PM
Post #6


The Innocent Shall Suffer, Big Time

Group Icon
Joined: 16 Aug, 2001
Posts: 6,124



Thanked 4 times

Dream Kudos: 515

Expert In: Python, Linux

My Contributions


T is the string you are searching in
P is the pattern string you are searching for


The outter loop steps through T character by character
The inner loop steps through P character by character

firs the outer loop, sets found to one (true), if in the inner loop, the
pattern doesnt match up, found is set to zero


The inner loop starts comparring P[j] with T[I+J] starting from I in T
do this for all J=size of P

If there is not a match on one, fail, and start that the next I in T.

If success, return I = the possition of the match
User is offlineProfile CardPM

Go to the top of the page

born2c0de
post 5 Dec, 2005 - 08:02 PM
Post #7


printf("I'm a %XR",195936478);

Group Icon
Joined: 26 Nov, 2004
Posts: 3,895



Thanked 34 times

Dream Kudos: 2800

Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions


You can use the strstr function from string.h
That will only tell you if a sub-string exists in a string and returns a pointer that points to the character where the match occurs.
You can subtract the pointer addresses to get the position of occurance.
But of course, I'd prefer the naive-string searcher algorithm.
This is an alternative.
User is offlineProfile CardPM

Go to the top of the page

Artaniz
post 7 Dec, 2005 - 11:00 AM
Post #8


D.I.C Head

**
Joined: 11 Apr, 2005
Posts: 127


My Contributions


ok i have studied the algorothm and have used it in my program and it works. thanks for all the help
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/20/08 12:46AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month