Huawei Machine Test-Find the word in the given string

Source: Internet
Author: User

Topic:

Finds the word in the given string (the word is made up of uppercase and lowercase alphabetic characters, and other non-alphabetic characters are treated as intervals of words, such as spaces, question marks, numbers, and so on; the other individual letters are not words) , and then output to a new string, and if a word recurs multiple times, output only once, and if the word is not found in the entire input string, output an empty string. The output words are separated by a "space", with no spaces after the last word.


Required implementation functions:
void My_word (charinput[], char output[])

"Enter" Char input[], enter the string
"Output" char output[], the output string
"Back" none

Example:
Input: charinput[]= "Some local buses, some1234123drivers",
Output: charoutput[]= "drivers local buses some"
Input: charinput[]= "%a^123 t 3453i* ()",
Output: charoutput[]= ""


#include <iostream> #include <string> #include <vector> #include <algorithm>using namespace std ; bool MyFunction (const string &str1,const string &str2) {return (Str2.length () < Str1.length ());} void My_word (char input[],char output[]) {vector<string> str;char*cur = Input;char*p = Input;int Len;bool flag =fals e;//remove impurities, take word while (*p! = ') ' {if ((*p >= ' a ' &&*p <= ' z ') | | (*p >= ' A ' &&*p <= ' Z ')) P++;else{len = p-cur;if (len >1) {string tmp (Cur,len); for (int i=0;i<str.size (); i++) if (tmp = = Str[i]) flag =true;if ( Flag!=true) Str.push_back (TMP);} cur = P+1;p++;flag =false;}} Len = p-cur;if (len >1) {string tmp (Cur,len); for (int i=0;i<str.size (); i++) if (tmp = = Str[i]) flag =true;if (Flag! = true) Str.push_back (TMP);} Sortsort (Str.begin (), Str.end (), myfunction);//splicing string *output= ' + '; int i;for (I=0;i<str.size () -1;i++) {strcpy ( Output+strlen (output), (str[i]+ ""). C_str ());} strcpy (Output+strlen (Output), (Str[i]). C_STR ()); Cout<<output<<endl;} int MAIn () {char input[]= "some ninja local buses, somes1234123drivers"; Char output[500];my_word (input, output); return 0;} 




Huawei Machine Test-Find the word in the given string

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.