To query a string with a pointer and replace the specified character in it

Source: Internet
Author: User
Tags count printf strlen
#define _crt_secure_no_warnings #include <stdio.h> #include <stdlib.h> #include &LT;STRING.H&G      
T A string that conforms to the following characteristics ("ABCD11111ABCD2222ABCDQQQQQ") requires writing a function (interface), outputting the following result//("ABCD"), and spreading the result;//More Highlights: http://      
                                www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg///Replace the string with (DCBA11111DCBA 2222DCBAQQQQQ) and spread the result. The number of occurrences//converted array is output//int getrestbuf (const char*buf1, int buflen, int *coun      
T, Char *buf2);      
    int getrestbuf (const char*buf1, int buflen, int *count, char *buf2) {int i=0;      
    int zcount = 0;      
    char *p1 = BUF1;      
    Char *psub = "ABCD";      
    Char buf[] = "DCBA";      
    This returns the number of occurrences//while (p1 = strstr (buf1//here should be the one you've picked up) Psub NULL) while (!= (P1=STRSTR))      
        {zcount++; Each time you find it, replace the string with "DCBA" or let the string correspond to the position you selected here is the assignment for (i = 0; I < sTrlen (psub);      
        i++) {* (p1 + i) = Buf[i];      
        } p1 = p1 + strlen (psub);      
        if (*p1 = = ' ") {break;      
    }//printf ("%d", p1);      
    } strcpy (Buf2, BUF1);      
    *count = Zcount;      
return 0;      
    } void Main () {int count = 0;      
    Char buf1[] = "ABCD11111ABCD2222ABCDQQQQQ";      
    int buflen = strlen (BUF1);      
    Char buf2[100];      
           
    Getrestbuf (BUF1, Buflen, &count, BUF2);      
    printf ("Find the number of strings is%d\n", count);      
    printf ("The original string is:%s\n", BUF1);      
    printf ("The replaced string is");      
    printf ("%s\n", buf2);      
System ("pause"); }

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.