Original question
Http://topic.csdn.net/u/20120710/21/1ac56385-6093-44ca-8ab1-c17d97ee6465.html? Seed = 418450313 & R = 79088627 # r_79088627
Wsprintf has three parameters. The first two are buffer and format. Buffer and format are provided. The format only contains the format "% s, how to output the string represented by every % S. For example, if the buffer is "abcdab1234" and the format is "% SAB % s34", the output substring should be "ABCD" and "12 ".
// strstr.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include "memory.h"#include "string.h"int main(int argc, char* argv[]){char ori[] = "abcdab1234";char format[] = "%sab%s34";char *p = format;char tmp[ 4 ][ 10 ];int count;int len;int i;memset( tmp, 0, sizeof( tmp ) );count = 0;len = 0;while( *p ){if( '%' == *p ){if( p != format ){count++;}len = 0;tmp[ count ][ len++ ] = '%';p++;tmp[ count ][ len++ ] = *p;len = 0;count++;}else{tmp[ count ][ len++ ] = *p;}p++;}count++;for( i = 0; i < count; i++ ){printf( "format part %d is --- %s \r\n", i + 1, tmp[ i ] );}char *q = ori;for( i = 0; i < count; i++ ){if( !strcmp( tmp[ i ], "%s" ) ){char *t = strstr( q + 1, tmp[ i + 1 ] );char dest[ 0x10 ] = "";memcpy( dest, q, ( int )( t - q ) );printf( "the %%s is %s \r\n", dest );q += strlen( dest );q += strlen( tmp[ i + 1 ] );}}return 0;}
format part 1 is --- %sformat part 2 is --- abformat part 3 is --- %sformat part 4 is --- 34the %s is abcdthe %s is 12Press any key to continue