String processing 3

Source: Internet
Author: User

1. Extract numbers from strings

2. Combine two strings into one string and output them. Use pointers to implement

3. Statistics of consecutive string letters

4. Delete the contained string

5. Replace string


Note: If you modify * char parameters or return the * result, remember to set a mark pointer to return the result, it is like marking the position of the head node * head for the return of the linked list.

========================================================== ========================================================== ======

1. Extract numbers from strings

Find the 0-9 character, count ++, and loop in it until it encounters a character that is not a number.


int take_num( const char *src,int outArr[] ){    assert(src!=NULL);    int count=0,tmp;    while(*src!='\0')    {        tmp=0;        if(*src>='0'&&*src<='9')        {            while(*src>='0'&&*src<='9')            {                tmp=tmp*10+*src-'0';                src++;            }                                                                                                                                                                                             outArr[count]=tmp;            count++;        }        src++;    }    printArrray(outArr,count);    return count;}

2. Combine two strings into one string and output them. Use pointers to implement

Generate a new string result --- use the address returned by an address record, and add '\ 0' after copying'


Const char * mergeString (const char * str1, const char * str2) {if (str1 = NULL) return (char *) str2; if (str2 = NULL) return (char *) str1; char * result, * address; result = new char [strlen (str1) + strlen (str2) + 1]; // every time a result is generated, add the relevant response address. Address = result;/* cout <str1 <str2 <endl; */while (* str1! = '\ 0') {* result ++ = * str1 ++;} while (* str2! = '\ 0') {* result ++ = * str2 ++;} * result =' \ 0'; cout <address <endl; return address ;}



3. Statistics of consecutive string letters


void countString( const char *str ){    assert(str!=NULL);    /*int *a=new int[52];*/    int a[52]={0};    while(*str!='\0')    {        a[*str-'A']++;        str++;    }    for(int i=0;i<52;i++)    {        if(a[i]!=0)            cout<<char(i+'A')<<a[i];    }}

4. Delete the contained string

It is mainly: tmp is assigned the length of the string to be deleted, used for string matching strcmp), find and process the result, count or src + = n Units)In other cases, assign values directly..

Int delete_substring (const char * src, const char * substring, char * result) {assert (src! = NULL & substring! = NULL); int count = 0; char * tmp = new char [strlen (substring) + 1]; // mark whether substring is contained. Char * pCpy = result; while (* src! = '\ 0') {if (* src = * substring) // You can also directly strncpy (tmp, src, strlen (substring) here )); then {strncpy (tmp, src, strlen (substring); * (tmp + strlen (substring) = '\ 0'; if (strcmp (substring, tmp) = 0) {// found, operate here. Core) count ++; cout <count <endl; src + = strlen (substring);} else {* pCpy = * src; pCpy ++; src ++ ;}} else {* pCpy = * src; pCpy ++; src ++ ;}} * pCpy = '\ 0'; // new string, '\ 0' indicates free (tmp); cout <"the processed string is: "<result <" the number of deletions is "<count <endl; return count ;}


5. Replace string


It is mainly: tmp is assigned the length of the string to be deleted, used for string matching strcmp), find and process the result, count or src + = n units), and assign values directly in other cases.


// 1 find the first replacement location: 2 tmp 3 part: replace-old with new-the content after the connection 3 tmp to replace the current string, loop // 1 abcddfabg (AB to xyz) 2 1-xyz-cddfabg 3 next round of loop .??? Char * replaceString (char * src, const char * oldstr, const char * newstr) {assert (src & oldstr & newstr); int lenstrlen (src ); int lenold = strlen (oldstr); int lennew = strlen (newstr); char * result, * tmp, * markNew; result = new char [lennew> lenold? Lensrc/lenold * lennew + 1: lensrc]; // maximum space memory: char * fanhui = result; // It is required to add the address of the returned pointer. Tmp = new char [lenold + 1]; cout <src <endl; while (* src! = '\ 0') {if (* src = * oldstr) {strncpy (tmp, src, lenold); tmp [lenold] =' \ 0 '; if (strcmp (tmp, oldstr) = 0) {// Replace the string here, and traverse the pointer to move lenold. The same number of replicas can be obtained here. MarkNew = (char *) newstr; while (* markNew! = '\ 0') {* result ++ = * markNew ++;} src + lenold;} else {* result ++ = * src ++ ;}} else {* result ++ = * src ++;} * result = '\ 0'; cout <fanhui <endl; return fanhui; // assert (src & oldstr & newstr); // char * pStr = src; // char * findLocation; // findLocation = strstr (pStr, oldstr ); // cout <findLocation <endl; // while (findLocation! = NULL) // {// char * tmp = (char *) malloc (strlen (pStr) + (strlen (newstr)-strlen (oldstr) + 1 ); // strncpy (tmp, pStr, findLocation-pStr); // tmp [findLocation-src] = '\ 0'; // strcat (tmp, newstr ); // strcat (tmp, findLocation + strlen (oldstr); // findLocation = strstr (tmp, oldstr); // cout <findLocation <endl; // cout <tmp <endl; // pStr = strdup (tmp); // free (tmp); // strcpy (pStr, tmp ); // copy, but the memory is insufficient: use the original character How can I keep the string longer ??? This is a problem with memory release. /// Cout <pStr <endl; // return pStr ;}


Test code:

Char * str1 = "abcdabac"; char * str2 = "AB"; char * result = new char [strlen (str1) + 1]; delete_substring (str1, str2, result ); char * strin = "af52d78d56dd89d"; int * outArray = new int [strlen (strin)]; // int outArray [] = new int [strlen (strin)]; // incorrect. new returns the * int type take_num (strin, outArray); char * srcstr = "22 abcdabokabhaha"; char * newstr = "xyz "; char * oldstr = "AB"; replaceString (srcstr, oldstr, newstr);/* char * str1 = "nihao"; char * str2 = "xyz"; mergeString (str1, str2); */char * str = "aaabbbbDDdjingianigndai"; countString (str );


Related Article

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.