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.....

Recommended Answers

All 4 Replies

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

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()! ;)

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". :)

remove "- lensub from the for loop

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.