Title Description:
Enter any string sequence through the keyboard, the string may contain multiple substrings, and the substrings are separated by a space. Write a program that automatically separates each substring and uses ', ' to separate it, and at the end adds a ', ' and stores the substring.
If you enter "ABC def GH i d", the result will be abc,def,gh,i,d,
Required Implementation functions:
void dividestring (const char *PINPUTSTR, long Linputlen, Char *poutputstr);
"Input" PINPUTSTR: input string
Linputlen: Input string length
"Output" POUTPUTSTR: output string, space has been opened up, and input string length;
"Note" Only needs to complete the function algorithm, the middle does not need any IO input and output
Example
Input: "ABC def GH i d"
Output: "Abc,def,gh,i,d,"
#include <iostream> #include <string>using namespace std;//sub-string separation void dividestring (const char* PINPUTSTR, Long Linputlen, char* poutputstr) { int i,j=0; BOOL Flag; for (i = 0; Pinputstr[i] = = "; ++i) //skip string preceded by Space continue; Flag = true; for (; i < Linputlen; ++i) { if (pinputstr[i]! = ') { if (!flag) flag = true; poutputstr[j++] = pinputs Tr[i]; Detach each substring and save it} else { if (flag) poutputstr[j++] = ', '; Flag = false; } } Poutputstr[j++] = ', '; Poutputstr[j++] = ' + '; } void Main () {char S[100];char result[100];while (gets (s)) {dividestring (S,strlen (s), result);cout<<result< <endl<<endl;} Cout<<endl;}
Test results, may not be thoughtful, welcome to check the leak:
Huawei Machine Test-sub-string separation