11: topic Overview
Enter a string of n characters. The characters are English characters.
Complete the following functions:
1) generate n character substrings based on the input string
Character substring generation rule: The Delimiter is a space. The character substrings are arranged in the same order as the original string and do not contain separators.
2) output the character substring of a sequence number as required. The sequence number ranges from 1 to 1 ~ N.
Example:
The input original string is "welcomeHuawei Software Training Camp! "
The output result must be "welcome" if the output string contains 1st characters"
The output result must be a string of 2nd characters"
If the output is a 6th-character substring, the output result should be "camp !"
Implement the following interface:
Interface 1: inputstring
Description : Enter the original string
Prototype : Void inputstring (char * pinstring );
Input Param : The original pinstring. The string bucket pointed to by pinstring will be released outside the function. Please apply for a bucket by yourself
Interface 2: findsubstring
Description : Output a specified string after sorting
Prototype : Void findsubstring (INT serialnumber, char ** ppoutstring)
Input Param : Serialnumber: from 1 ~ N is the character substring number.
Output Param: * The ppoutstring Pointer Points to the first address of the character substring. The string must end with '\ 0.
If the serialnumber is invalid, point * ppoutstring to an empty string.
Interface 3: Clear
Description : Clears the storage space of character substrings.
Prototype : Void clear (void)
# Include <iostream>
# Include <memory>
# Include "string"
# Include <vector>
Using namespace STD;
Static int num = 0;
Static char ** strarray = NULL;
Void setsubstringnum (char * pinstring)
{
While (pinstring)
{
If (* pinstring = ''| * pinstring = '\ 0 ')
Num ++;
If (* pinstring = '\ 0 ')
Return;
Pinstring ++;
}
}
// This function is well written.
Void setstringarray (char * pinstring)
{
Strarray = new char * [num];
Int subindex = 0, sublen = 0;
While (pinstring)
{
Sublen ++;
If (* pinstring = ''| * pinstring = '\ 0 ')
{
Strarray [subindex] = new char [sublen];
Memcpy (strarray [subindex], pinstring-sublen + 1, sublen );
Strarray [subindex] [sublen] = '\ 0 ';
Subindex ++;
Sublen = 0;
}
If (* pinstring = '\ 0 ')
Return;
Pinstring ++;
}
}
Void inputstring (char * pinstring)
{
Setsubstringnum (pinstring );
Setstringarray (pinstring );
}
// Pay attention to the processing method of ppoutstring and Its Relationship with real parameters.
Void findsubstring (INT serialnumber, char ** ppoutstring)
{
If (serialnumber> = 1 & serialnumber <= num)
* Ppoutstring = strarray [serialnumber-1];
Else
* Ppoutstring = NULL;
}
Void clear ()
{
For (INT I = 0; I <num; I ++)
Delete [] strarray [I];
Delete [] strarray;
}
Int main (){
Inputstring ("Welcome to Huawei software training camp! ");
Char * outstring;
// Note the second parameter
Findsubstring (3, & outstring );
Cout <outstring <Endl;
Char C;
Cin> C;
Return 0;
}