String processing 2

Source: Internet
Author: User

1. Write a function int fun (char * p) to determine whether a string is a background bool isHuiwen (const char * str );

2 string reverse order: char * inverseString (char * str );

3. Find the character with the highest frequency in the string: char maxCountChar_string (const char * str );

4. Write the code to find the occurrence times of the substring in the parent string: int subNum_string (const char * str, const char * substring );

5. Compile a function to shift n loops of a char string to the right. For example, if the original is "abcdefghi", if n = 2, it should be "hiabcdefgh": void loopMoveNum (char * pStr, int steps );

6. Find the longest symmetric string in the string: Start from the longest -- char * maxLenHuiwen (char * pStr );

7. Calculate the number of symmetric strings in the string and the longest symmetric string): int countHuiwen (const char * pStr );


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

1. Write a function int fun (char * p) to determine whether a string is a background bool isHuiwen (const char * str );

// (Str [I], str [len-1-i]) Compare bool isHuiwen (const char * str) {///1 in the form of a character array. // Assert (str! = NULL); // bool isTrue = true; // int len = strlen (str); // for (int I = 0; I <len/2; I ++) // {// if (str [I]! = Str [len-1-i]) // {// cout <"bushi huiwen" <endl; // isTrue = false; // break; ///} // return isTrue; // 2 use the pointer format. Assert (str! = NULL); bool isTrue = true; int len = strlen (str); char * q = (char *) str + len-1; // for pointers with const, the form (cahr *) is required. While (str <q) {if (* str! = * Q) {isTrue = false; cout <"bushi huiwen" <endl; break;} str ++; q --;} return isTrue ;}

2 string reverse order: char * inverseString (char * str );

// (Str [I], str [len-1-i]) Exchange char * inverseString (char * str) {// 2 using the pointer form. // Assert (str! = NULL); // int len = strlen (str); // char * q = (char *) str + len-1; // for pointers with const, the form (cahr *) is required. // Char * add = str; // char tmp; // while (str <q) // {// tmp = * str; // * str = * q; // str ++; // move closer to the center. // Q --; //} // cout <"the converted string is:" <add; // return add; assert (str! = NULL); int len = strlen (str); char tmp, * add = str; for (int I = 0; I <len/2; I ++) {tmp = str [I]; str [I] = str [len-1-i]; str [len-1-i] = tmp;} cout <"the converted string is: "<add <endl; return add ;}

3. Find the character with the highest frequency in the string: char maxCountChar_string (const char * str );

// Integration can be implemented using map and so on.) char maxCountChar_string (const char * str) {assert (str! = NULL); int allchar [26] = {0}; // assume 26 lower-case letters. What about other letters ??? Important: int maxNum = 1; char * t = (char *) str, maxchar = * str; while (* t! = '\ 0') {allchar [* t-'a'] ++; if (allchar [* t-'a']> maxNum) {maxchar = * t; maxNum = allchar [* t-'a'];} t ++ ;} cout <"The max number of char is" <maxchar <"---" <maxNum <endl; return maxchar ;}

4. Write down the occurrence times of the substring in the parent string: int subNum_string (const char * str, const char * substring );

// Add the counting function based on String Matching: recursive can be used, right ??? Int subNum_string (const char * src, const char * dest) {assert (src! = NULL & dest! = NULL); int len1, len2; len1 = strlen (src); len2 = strlen (dest); int count = 0; for (int I = 0; I <len1; I ++) {for (int j = 0; j <len2; j ++) {if (src [I + j]! = Dest [j]) {break; // just roll out the inner loop} if (j = len2-1) {// I + len2-1 = I + j/* printf ("The location is % d -- % d \ n", I, I + j); */count ++; cout <"no." <count <"string location" <I <"--" <I + j <endl; j = 0; break; // jump out of the cycle of the inner layer; otherwise, the wireless loop goes down. }}cout <"Total" <count <"String !!! "<Endl; return count ;}


5. Compile a function to shift n loops of a char string to the right. For example, if the original is "abcdefghi", if n = 2, it should be "hiabcdefgh": void loopMoveNum (char * pStr, int steps );

Void loopMoveNum (char * pStr, int steps) {// 1 memcpy: 1. Move the front of tmp 2 to the back of tmp and add the position. // Int lenRest = strlen (pStr)-steps; // char * tmp; // tmp = (char *) malloc (steps + 1) * sizeof (char )); // assert (tmp! = NULL); // heheei123 ---- 123 (tmp) ---- heh heheei (pStr) ---- 123 heheei (pStr) // memcpy (tmp, pStr + lenRest, steps ); // memcpy (pStr + steps, pStr, lenRest); // memcpy (pStr, tmp, steps); // free (tmp); // tmp = NULL; // cout <"the string after right movement is" <pStr <endl; // 2 is obtained using strcpy. Int lenRest = strlen (pStr)-steps; char * tmp; tmp = (char *) malloc (steps + strlen (pStr) + 1) * sizeof (char )); assert (tmp! = NULL); // heheei123 ---- 123 (tmp) ---- 123 heheei123 (tmp) ---- 123 heei strcpy (tmp, pStr + lenRest); strcpy (tmp + steps, pStr ); * (tmp + strlen (pStr) = '\ 0'; strcpy (pStr, tmp); free (tmp); tmp = NULL; cout <"the string after right movement is" <pStr <endl ;}

6. Find the longest symmetric string in the string: Start from the longest -- char * maxLenHuiwen (char * pStr );

// Start from the longest start. If you know it, exit the loop and save the starting position and length. Char * maxLenHuiwen (char * pStr) {assert (pStr! = NULL); int lenStr = strlen (pStr); if (lenStr = 1) {cout <"only one character, dear !! "<Endl; return pStr;} char * tmp; // stringProcess strProc; for (int n = lenStr; n> 1; n --) {for (int m = 0; m <= lenStr-n; m ++) {tmp = new char [n + 1]; memcpy (tmp, pStr + m, n); * (tmp + n) = '\ 0'; // strProc. strncpy1 (tmp, pStr + m, n); // use a self-compiled class, which is of great significance. If (isHuiwen (tmp) {cout <"the largest symmetric string is" <tmp <", and the length is" <n <endl; return tmp ;}} // delete [] tmp;} cout <"sorry, no symmetric string found !!! "<Endl; return NULL ;}

7. Calculate the number of symmetric strings in the string and the longest symmetric string): int countHuiwen (const char * pStr );

// From small to large, or from large to small): Find the number of one added, instead of break; int countHuiwen (const char * pStr) {assert (pStr! = NULL); int count = 0; int len = strlen (pStr); if (len = 1) {cout <"only one character, dear !! "<Endl; return 1;} char * tmp; for (int num = 2; num <= len; num ++) // num: length of symmetric string {for (int m = 0; m <= len-num; m ++) {tmp = new char [num + 1]; // memcpy (tmp, pStr + m, num); strncpy (tmp, pStr + m, num); * (tmp + num) = '\ 0'; if (isHuiwen (tmp )) {count ++; cout <"no." <count <"symmetric string is" <tmp <", starting from" <m <", the length is <num <endl ;}delete [] tmp ;}cout <"the number of symmetric strings is:" <count <endl; return count ;}


Test code

int main(){    //char str[]="heeheei123";    //isHuiwen(str);    //inverseString(str);    /*maxCountChar_string(str);*/    /*char str1[]="hahahanihaohah";    char str2[]="hah";    subNum_string(str1,str2);*/    //loopMoveNum(str,3);    char str[]="heehee";    //maxLenHuiwen(str);    countHuiwen(str);    return 0;}


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.