1.11M Members

finding substring within the string

 
0
 

please can any one write the code without using pointers and in a simplest way...
my question is finding substring position in main string .....these both strings you have to take at the run time and dont use any library methods.....you yourself have to write the code.....please give eply ASAP.....

 
0
 

>>please can any one write the code without using pointers
I don't think that's possible.

 
0
 

I would use logic like this:

[This is not "code", it's pseudo code, in C format. Difficult to describe it accurately, without code like syntax, imo.]

/* you can count these yourself, without using strlen(), if necessary */
lensub = strlen(substring);
lenstr = strlen(string);

for(i=0;i<(lenstr - lensub);i++) {  
  j=0;
  while(string[i] == substring[j] {
    j++;
    i++;  
}
  /* now if --j == lensub  you've found a substring so
     break out of the for loop, or not, depending on 
     whether you want to find the first substring in the
     string, or find all the substrings in the string.

     decrement i, or you'll miss a letter in the string, if you're 
     searching for more substrings.
  */
}

Certainly not as easy as using strstr()! ;)

 
0
 

Logic works OK, but don't decrement j. Using another variable like j, inside the while loop, makes it a bit easier.

sujathaarsid, be *sure* your next post in this thread shows your additional work on this program, not just "help". :)

 
0
 

remove "- lensub from the for loop

add "and j<lenstr" to the while test condition

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article