5. Several relatively complex procedures in string operations

Source: Internet
Author: User

1, Write a function to implementCharReturns the right of a string.NBit. For exampleAbcdehi, n = 2,ThenHiabcde.

ExampleCode

 
# Include "iostream" using namespace STD; const int max_len = 20; void loopmove (char * cpstr, int isteps) {// note that during the entire process, the final characters of cpstr do not involve processing char ctemparray [max_len]; size_t szstrlength = strlen (cpstr); size_t in = szstrlength-isteps; memcpy (ctemparray, cpstr + in, isteps ); memcpy (ctemparray + isteps, cpstr, In); memcpy (cpstr, ctemparray, szstrlength); ctemparray [szstrlength + 1] = '\ 0 '; cout <ctemparray <Endl;} int main () {char ctemp [] = "abcdefghi"; loopmove (ctemp, 2); cout <ctemp <Endl; return 1 ;}

2, Enter a line of strings, find the same and longest string, output it and its first character position. For exampleYyabcdabjcabceg,The output isABC, 3.

General idea: Set the stringYyabcdabjcabcegDisassembling:

Yyabcdabjcabceg

Yabcdabjcabceg

Abcdabjcabceg

...

CEG

Eg

G

Sort the strings, compare the precursor of adjacent strings, and find the longest common precursor.

In ourProgramWe do not use this method because it takes a lot of time in sorting. We borrowedC ++Implementation FunctionsFind.

Note:Basic_string: substr

Basic_string substr (size_type Pos = 0, size_type n = NPOs) const;

The member function returns an object whose controlled sequence is a copy of up to n elements of the controlled sequence beginning at position pos.

Returns a substring of the specified length starting from the specified position.

Parameters

PosRequired. The starting position of the required substring. The index of the first character in the string is0.

NOptional. The number of characters in the returned substring.

Note ifNIs0Or a negative number. An empty string is returned. If this parameter is not specified, the sub-string continues to the end of the string.

InVSTesting, if yesNIs a negative number or greater than the total length of the Main string, the output isPosStart to the end of the main string.

Sample Code

# Include "iostream" # include "string" using namespace STD; int main () {string strinput; cout <"input a string:" <Endl; CIN> strinput; string strtemp; For (size_t I = strinput. length ()-1; I> 1; I --) {for (size_t J = 0; j <strinput. length (); j ++) {If (I + J) <= strinput. length () {size_t szforw = 0; size_t szbacw = 0; strtemp = strinput. substr (J, I); szforw = strinput. find (strtemp); szbacw = strinput. R Find (strtemp); If (szbacw! = Szforw) {cout <strtemp <"" <szforw + 1 <Endl; return 0 ;}}} return 1 ;}

3, ImplementationStrstr ()Function. If the main string is12345678,Substring is234,Returns2345678.

Sample Code

# Include "iostream" # include "string" using namespace STD; const char * strstr1 (const char * strmainstring, const char * strsubstring) {for (size_t I = 0; strmainstring [I]; I ++) {size_t itempj = 0; size_t itempi = I; If (strmainstring [itempi] = strsubstring [itempj]) {While (strmainstring [itempi ++] = strsubstring [itempj ++]) {If (strsubstring [itempj] = '\ 0 ') return & strmainstring [I] ;}} return NULL;} int main () {char str1 [] = "12345678"; char str2 [] = "234 "; const char * str3 = strstr1 (str1, str2); cout <str3 <Endl; return 1 ;}

4Put the words in a sentence upside down without changing the punctuation marks. For example,I come from Tianjin.",Change to"Tianjin. From come I".

General idea: Adjust the entire string and then adjust each word.

Sample Code

# Include "iostream" # include "string" // # include "functional" // # include "algorithm" using namespace STD; int main () {cout <"input a string:" <Endl; string strofaline; Getline (CIN, strofaline); size_t szstrlength = strofaline. length (); size_t sztempbeg = 0; size_t sztempend = szstrlength-1; // The first step is global switching while (sztempbeg <sztempend) {swap <char> (strofaline [sztempbeg ++], strofaline [sztempend --]);} // step 2, partial Si Switching Ze_t sztempi = 0; while (strofaline [sztempi]) {If (strofaline [sztempi]! = '') {Sztempbeg = sztempi; while (strofaline [sztempi]! = '\ 0' & strofaline [sztempi]! = '') {Sztempi ++;} sztempi --; sztempend = sztempi;} while (sztempbeg <sztempend) {swap <char> (strofaline [sztempbeg ++], strofaline [sztempend --]);} sztempi ++;} cout <strofaline <Endl; return 1 ;}

5Find the substring that appears the most frequently in a string.

General idea: for exampleAbcbcbcabc, Then the string is cut:

Abcbcbcabc

Bcbcbcabc

Cbcbcabc

Bcbcabc

Cbcabc

Bcabc

...

BC

C

Then, from the first substring to the last substring, match the current substring with each substring and make statistics.

Sample Code

# Include "iostream" # include "string" # include "vector" // # include "functional" // # include "algorithm" using namespace STD; pair <int, string> staticnum (const string & Str) {vector <string> substrs; // stores the cut string substr; int imaxcount = 1, icount = 1; size_t itempi, ilen = Str. length (); For (itempi = 0; itempi <ilen; itempi ++) {substrs. push_back (Str. substr (itempi, ilen-itempi);} For (itempi = 0; itempi <ilen; itempi ++) {for (size_t itempj = itempi + 1; itempj <ilen; itempj ++) {icount = 1; if (substrs [itempi]. substr (0, itempj-itempi) = substrs [itempj]. substr (0, itempj-itempi) {icount ++; // compare each of the subsequent substrings. itempj-itempi is the length of the string to be compared. // note, the duration is itempj-itempifor (size_t itempk = itempj + (itempj-itempi); itempk <ilen; itempk + = itempj-itempi) {If (substrs [itempi]. substr (0, itempj-itempi) = substrs [itempk]. substr (0, itempj-itempi) {icount ++;} else // The subsequent strings are not consecutive {break ;}} if (icount> imaxcount) {imaxcount = icount; substr = substrs [itempi]. substr (0, itempj-itempi) ;}}return make_pair (imaxcount, substr) ;}int main () {string STR; pair <int, string> RS; string strtemp = "abcbcbcabc"; RS = staticnum (strtemp); cout <Rs. first <"" <Rs. second <Endl; return 1 ;}

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.