O
Your mission says:Delete the first word of the intended line. Separation of words
gapWhat is the word in the context of the assignment? It's a sequence of symbols not including built-in gaps, is it?
What's the first word? It's the first sequence of the signs in the row that doesn't start from a break, is it?Therefore, in order to correctly solve the assignment, you must (1) miss the main gaps in the row; (2) find the first unstable symbol. It would mean the beginning of the first word in the line; (3) to find the end of the first word, that is, to find the first blank symbol further in the line; (4) to remove the leading gaps before the second word in the line.The last paragraph is not necessary. We can leave the main gaps before the second word, because the mission says that only the first word should be deleted. But even here, the question arises, but is it necessary to remove the main gaps before the first word itself, or should they be retained in the exit line?It is important to formulate a mission so that there is no ambiguity.Three approaches are outlined below.The first approach removes the first word and all the leading gaps before the first and second words (for this, I added an initial gap for visibility).The second approach removes the first word and the main gaps that precede only the first word.In the third approach, only the first word is deleted. All gaps remain in the exit line.#include <iostream>
#include <cstring>
int main()
{
{
// Removing the first word and all leading spaces
const size_t N = 80;
char str[N] = " faE,1*fe A.,.3iBVf Oq. e.of 43G.FW2,jiq,e[ qe";
char token[N];
size_t n = std::strspn( str, " " );
n += std::strcspn( str + n, " " );
n += std::strspn( str + n, " " );
std::strcpy( token, str + n );
std::cout << '\"' << str << '\"' << std::endl;
std::cout << '\"' << token << '\"' << std::endl;
}
std::cout << std::endl;
{
// Removing the first word and all leading spaces that precede the first word
const size_t N = 80;
char str[N] = " faE,1*fe A.,.3iBVf Oq. e.of 43G.FW2,jiq,e[ qe";
char token[N];
size_t n = std::strspn( str, " " );
n += std::strcspn( str + n, " " );
std::strcpy( token, str + n );
std::cout << '\"' << str << '\"' << std::endl;
std::cout << '\"' << token << '\"' << std::endl;
}
std::cout << std::endl;
{
// Removing only the first word and keeping all embedded spaces
const size_t N = 80;
char str[N] = " faE,1*fe A.,.3iBVf Oq. e.of 43G.FW2,jiq,e[ qe";
char token[N];
size_t n = std::strspn( str, " " );
strncpy( token, str, n );
size_t m = n;
n += std::strcspn( str + n, " " );
std::strcpy( token + m, str + n );
std::cout << '\"' << str << '\"' << std::endl;
std::cout << '\"' << token << '\"' << std::endl;
}
return 0;
}
The programme has the following conclusion:" faE,1*fe A.,.3iBVf Oq. e.of 43G.FW2,jiq,e[ qe"
"A.,.3iBVf Oq. e.of 43G.FW2,jiq,e[ qe"
" faE,1*fe A.,.3iBVf Oq. e.of 43G.FW2,jiq,e[ qe"
" A.,.3iBVf Oq. e.of 43G.FW2,jiq,e[ qe"
" faE,1*fe A.,.3iBVf Oq. e.of 43G.FW2,jiq,e[ qe"
" A.,.3iBVf Oq. e.of 43G.FW2,jiq,e[ qe"
This conclusion shows the differences between the three approaches to the assignment.