"A string question" is a silly solution.

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.