Monday, May 24, 2010

Find a string in a given string in microsoft visual c++ w/o using the strstr() function or the string.find...?

how can i search a string in a given string by not using the strstr() fxn...





ex:


text1=ueccss


text2=ccss





output: text2 is contained in text1





**otherwise, not contained....





and i must not use a pointer,,,,,???


is it possible for only a single-dimension array??

Find a string in a given string in microsoft visual c++ w/o using the strstr() function or the string.find...?
this is in C


but you should understand it





int counter;


int counter2;


int counter3;


int found = 0;





for ( counter = 0; counter %26lt; strlen(text1) ; counter++) {


if (text2[0] == text1[counter]) {


// a character in text1 matches the first character in text2


// lets see if its a match


counter3 = counter + 1; // temporary counter


for ( counter2 = 1; counter2 %26lt; strlen(text2); counter2++, counter3++) {


if (text2[counter2] != text1[counter3]) {


found = 0;


break;


}


}


if (found) {


printf("string found\n");


break;


}


}





here is the logic:


Loop thru each character of text1 comparing the characters with the first character of text2. If there is a match, loop thru text2 comparing each character with text1 starting at the position or index of the first char that matched the first character in text2. But since we already compared the first char's , you actually start the second comparison loop at the second chars.





the second chars in this case would be:





c[this one-%26gt; c]ss for text2


and uec[this one-%26gt; c]ss for text1


No comments:

Post a Comment