Misc string test

Source: Internet
Author: User
Tags strtok
/** STD: String Detailed Description * Visual Studio 2008sp1, use Ctrl + F5 to start debugging */# include <iostream> # include <string> # include <cstdio> # include <cstddef> # include <cstring> # include <cstdlib> // qsort # include <errno. h>/* error codes */# include <numeric> # include <algorithm> using namespace STD; // # DEFINE _ crt_secure_no_warnings with/D # pragma warning (Disable: 4996) // disable stupid warning of this function or variable may Be unsafe. // consider using strerror_s instead # define maxlen 1000 char line [maxlen]; int Getline (char s [], int Lim) {int CH = 0, I; I = 0; while (-- Lim> 0 & (CH = getchar ())! = EOF) & Ch! = '\ N') s [I ++] = CH; If (CH =' \ n') s [I ++] = CH; s [I] = '\ 0'; return I;} // raiseint CMP (const void * a, const void * B) {return * (char *) b-* (char *) A;} int main (INT argc, char * argv []) {const basic_string <char> S1 ("test something "); string S2 ("test"); // The first member function C-stringstring stra ("hello"); const char * cstra = "C-string"; stra. append (cstra); cout <"appending the C-string cstra to string stra Gives: "<stra <Endl; // The second member functionconst char * cstrb =" Some ELSD "; stra. append (cstrb, 2); cout <stra <Endl; cout <"input a character string" <Endl; If (Getline (line, maxlen) <1) Exit (1); // quick sortcout <"before line:" <line <Endl; qsort (line, sizeof (line) /sizeof (line [0]), sizeof (line [0]), CMP); cout <"Quick Sort line:" <line <Endl; /// string test cout <"test string ss. The following is the SS string: "<Endl; string SS (" maybe you are long girl, I'm who are you.12563. "); cout <SS <Endl; cout <ss. find_first_not_of ('') <Endl; // There are many spaces in front of it to find the first cout without spaces <ss. find_first_not_of ("abcdefghijkmno") <Endl; // return the first cout element not in the specified character set <ss. find_last_not_of ("abcdefghijkmno") <Endl; cout <ss. find ("longk") <Endl; // If NO content is found in string, it searches for cout beyond the range <"to calculate the number of o in string SS: "<count (ss. begin (), SS. end (),' O') <Endl; cout <"string SS length:" <ss. size () <", string SS letters and numbers:" <count_if (ss. begin (), SS. end (), isalnum) <Endl; // condition comparison, ctype. h isalnum determine the letter or number ////////////////////////////////// //////////////////////////////////////// // strcspn & strspncout <Endl; const char * psztest = "Long long ago, there is girl, she \ 'name is little RedHat"; cout <"\ r \ n test strcspns () function, string psztest: "<psztest <End L; cout <"Length:" <strlen (psztest) <"--- firt_not_of length 'xyza '" <strcspn (psztest, "xyza") <Endl; //////////////////////////////////////// /// // test strtokchar STR [] = "now # is the time for all #### good men to come to the # aid of their country \ 0 "; char * delims = "#"; char * token = NULL; cout <"\ n test strtok function (in Linux, use the Function · char * strsep (char ** _ stringp, const char * _ delim )· ), "String to be tested: \ n" <STR <Endl; token = strtok (STR, delims); // thread-insecure function, 'str' outside the column is damaged while (Token! = NULL) {printf ("% s \ n", token); token = strtok (null, delims );} //////////////////////////////////////// /// // memchrcout <" \ n test function void * memchr (void * PTR, int value, size_t num); \ r \ n "<Endl; char * PCH; int ch; CH = 'E'; strcpy (STR, "now # is the time for all #### good men to come to the # aid of their country"); cout <"the test STR: \ n "<STR <Endl; PCH = (char *) memchr (STR, CH, Strlen (STR); If (PCH! = NULL) {printf ("> character \ '% C \' is found at % d. \ n ", CH, PCH-str + 1); printf (" >>% s \ n ", PCH);} else {printf (" fuck, character \ '% C \' is not found! \ N ", CH) ;}cout <" \ n "<Endl; //////////////////////////////////////// /// // strerrorcout <" test strerror (), create an error: press any key continue... "<Endl; cin. get (); // For (INT err = 1; err <42; err ++) {// printf ("error code % d: % s \ n", err, strerror (ERR); //} file * file; file = fopen ("unexist. file "," R "); If (file = NULL) printf (" Open File crashed, error code % d: % s \ n ", errno, strer Ror (errno )); //////////////////////////////////////// /// // strcasecmpcout <"\ n test strcasecmp () function. Enter an animal! (Dog, cat, Etc ...) "<Endl;/* use your own C function */If (Getline (line, maxlen)> 1) {line [strlen (line) -1] = '\ 0'; // delete new line/* Do Something compare */If (stricmp (line, "dog") = 0) {// stricmp references string in essence. the strcasecmp () function in H, pitfall printf ("dog is very interesting .... en? \ N ");} else if (stricmp (line," cat ") = 0) {printf (" Actually, I do not like cats. \ n ");} else if (stricmp (line," cow ") = 0) {printf (" Cattle (colloquially cows) are the most common type of large domesticated ungulates. "" They are a prominent modern Member of the subfamily bovinae, are the most widespread "" species of the genus Bos, and are most commonly classified collectively as Bos primigenius. "" Cat Tle are raised as livestock for meat (beef and veal), as dairy animals for milk and "" other dairy products, and as draft animals (oxen/bullocks) (pulling carts, plows and "" the like ). other products include leather and dung for manure or fuel. in some countries, "" such as India, cattle are sacred. from as few as eighty progenitors domesticated in "" Southeast Turkey about 10,500 years ago, it Is estimated that there are now 1.3 billion "" cattle in the world today. \ n ");} else if (stricmp (line," pig ") = 0) {printf (" a pig is any of the animals in the genus Sus, within the suidae family of even-toed "" ungulates. pigs include the domestic pig, its ancestor the wild boar, and several other "" wild relatives. pigs are omnivores and are highly social and intelligent animals. \ n ");} else {printf (" % S ,? What animal it was ?? \ N ", line) ;}// use the istream object, STD: String convert to C-style stringcout <" enter something \ n "<Endl; STD: String szline; STD: Getline (STD: Cin, szline); // don't be STD: cinstrcpy (line, szline. c_str (); printf ("> % s \ n", line ); //////////////////////////////////////// /Let's take a look at the combination of typedef and const, typedef STD:: string * pstring; // const pstring mystring; // error. Because the mystring variable is of the const type, initialize // const int JJ; // errorstd :: string mystr1 ("got some string here. "); STD: String mystr2 (" Wow, I got the second string. "); const pstring mystring = & mystr1; // mystring = & mystr2; // error pointer mystring only points to the cout of mystr1 <" mystring: \ n "<* mystring <Endl; mystring-> append (" Some append string. "); cout <" after append of mystring: \ n "<* mystring <Endl; return 0 ;}

Output result

Appending the C-string cstra to string stra gives: Hello C-stringhello C-stringsoinput a character stringlong long ago, there is girl, she's name is Little RedHat .. before line: Long long ago, there is girl, she's name is Little RedHat .. quick Sort line: ttttttssrrrooonnnmllllliiiihhhggggeeeeeedaaa ..,, 'test string SS, the following is the SS string: Maybe you are long girl, I'm who are you.12563.40554294967295 calculate string Number of o in SS: 5 string SS length: 56, letter and number in string SS: 39 test strcspn () function, string to be tested psztest: Long long ago, there is girl, she 'name is little RedHat length: 55 --- firt_not_of length 'xyza '10 test the strtok function (in Linux, use the function char * strsep (char ** _ stringp, const char * _ delim )·), string to be tested: now # is the time for all #### good men to come to the # aid of their countrynowis the time for Allgood men to come to theaid of their country test function v Oid * memchr (void * PTR, int value, size_t num); the test STR: now # is the time for all #### good men to come to the # aid of their country> character 'E' is found 10.> e time for all #### good men to come to the # aid of their country test strerror (), create an error: press any key continue... open File crashed, error code 2: no such file or directory test the strcasecmp () function. Enter an animal! (Dog, cat, Etc ...) PIGA pig is any of the animals in the genus Sus, within the suidae family of even-toed ungulates. pigs include the domestic pig, its ancestor the wild boar, and several other wild relatives. pigs are omnivores and are highly social and intelligent animals. enter something LOL. file> LOL. filemystring: Got some string here. after append of mystring: Got some string here. some append string. press 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.