1,105,312 Community Members

finding substring within the string

Member Avatar
sujathaarsid
Newbie Poster
6 posts since Jul 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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.....

Member Avatar
Ancient Dragon
Achieved Level 70
27,573 posts since Aug 2005
Reputation Points: 5,231 [?]
Q&As Helped to Solve: 3,028 [?]
Skill Endorsements: 115 [?]
Team Colleague
Featured
Sponsor
 
0
 

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

Member Avatar
Adak
Posting Virtuoso
1,711 posts since Jun 2008
Reputation Points: 419 [?]
Q&As Helped to Solve: 207 [?]
Skill Endorsements: 10 [?]
 
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()! ;)

Member Avatar
Adak
Posting Virtuoso
1,711 posts since Jun 2008
Reputation Points: 419 [?]
Q&As Helped to Solve: 207 [?]
Skill Endorsements: 10 [?]
 
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". :)

Member Avatar
Adak
Posting Virtuoso
1,711 posts since Jun 2008
Reputation Points: 419 [?]
Q&As Helped to Solve: 207 [?]
Skill Endorsements: 10 [?]
 
0
 

remove "- lensub from the for loop

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

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