Monday, July 27, 2009

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





can sum1 help me??

Find a string in a given string in microsoft visual c++ w/o using the strstr() function or the string.find...?
If you can't use those functions, just go through character by character and see if the string is contained:





// argv[1] is the string you're going to search through


// argv[2] is the string you're trying to find in argv[1]


int main( int argc, char **argv ) {


int str1len = strlen( argv[1] );


int str2len = strlen( argv[2] );


int i = 0;


int j = 0;


int found = 0;





for( i = 0; i %26lt; str1len; i++ ) {


if( argv[1][i] == argv[2][0] ) {


for( j = 0; j %26lt; str2len; j++ ) {


if( argv[1][i + j] != argv[2][j] ) break;


}


if( j == str2len ) {


found = 1;


break;


}


}


}





if( found ) printf( "text2 is contained in text1\n" );


else printf( "not contained\n" );





return( 0 );


}


No comments:

Post a Comment