char str[80] = "gre qg1 256g#r4 6c aegev e gea, =2g +rd*gf g3t523gr"; char token[80]; char *p, *t; cout << str << '\n'; p = str; //считываем строку. while (*p){ t = token; //считываем строку до первого пробела и копируем в строку в token while (*p != ' ' && *p){ *t = *p; ++t; ++p; } //если встретился пробел перемещаемся на следующюю позицию if (*p) ++p; } cout << token << endl; 

Closed due to the fact that off-topic participants Maxim Kamalov , Alexey Shtanko , aleksandr barakin , user26699, Ella Svetlaya 6 Jun '15 at 16:25 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • "Questions asking for help with debugging (" why does this code not work? ") Should include the desired behavior , a specific problem or error, and the minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, complete, repeatable example . " - Maxim Kamalov, Alexey Shtanko, aleksandr barakin, Community Spirit, Ella Svetlaya
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • What is the question? Is there something wrong with the code? - user6550
  • Yes, I can not understand - Ilya Tikhonov
  • Why else nested loop? Just while (* p ++) {if (* p! = '') * T ++ = * p;} - Alexey Sarovsky
  • You can’t understand it because it’s somehow difficult to invent everything :) - user6550
  • Now we will try - Ilya Tikhonov

2 answers 2

Try to say the algorithm. Words can be out loud. Write pseudocode on a piece of paper with a pencil and "walk" it:

 пока есть символы в строке если символ не пробел скопировать по назначению увеличить указатель назначения конец если увеличить указатель источника конец пока 

And write it in C, literally:

 static char * rm_spaces( char * src ) { char * from = src; char * to = src; while( *from ) { if( *from != ' ' ) { *to = *from; to++; } from++; } *to = 0; return src; } 

Works? Then you can optimize.

  • Yes, it works. - Ilya Tikhonov
  • * to = 0; What does this line mean, can you explain with words? - Ilya Tikhonov
  • one
    Since such a question arises, it is still better to first read some textbook on C of the most elementary level ... And get to the chapter on the C-line. - user6550
 char str[80] = "gre qg1 256g#r4 6c aegev e gea, =2g +rd*gf g3t523gr"; //строка символов char token[80]; char *p, *t; cout << str << '\n'; p = str; t = token; while (*p){ if (*p != ' '){ *t = *p; ++t; } ++p; } cout << token << '\n'; 

So I redid it in main-not, while the functions are poorly understood, why do I have extra characters in the line?

  • Because there is no terminating zero in token . A note about the debugger was ignored, right? But with him it would immediately become clear. - user6550
  • If you have a poor understanding of the function, then first sort it out. Because if you do not know the basics of the language, you need to get a fair deuce from the teacher, and read a book. Do not expect that "so a ride." Idlers in the programmers do not take. - VladD
  • where is the teacher and the student? - Ilya Tikhonov
  • Well, since there are essentially no complaints, then at least you can start from here :) lib.ru/CTOTOR/starterkit.txt Better from the very beginning and step by step . But if you can not wait, then twist to the words "Strings are arrays of LETTERS". But then still go back to the beginning and take steps. - user6550 7:08 pm