Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > C/C++: Общие вопросы > как разделить строку ?


Автор: admsasha 5.6.2005, 15:18
есть строка
str= char[100];

"qwe#ewqe"

как получить левую и правую часть ?

Автор: Mayk 5.6.2005, 17:29
Самый простой способ:
Код

char* right = strchr(str,'#');
if(right)
    *right++=0;

str - левая часть,
right - правая

Автор: bel_nikita 5.6.2005, 23:56
Можно strtok попробывать
Код

Example
// crt_strtok.c
/* In this program, a loop uses strtok
 * to print all the tokens (separated by commas
 * or blanks) in the string named "string".
 */

#include <string.h>
#include <stdio.h>

char string[] = "A string\tof ,,tokens\nand some  more tokens";
char seps[]   = " ,\t\n";
char *token;

int main( void )
{
   printf( "Tokens:\n" );
   /* Establish string and get the first token: */
   token = strtok( string, seps );
   while( token != NULL )
   {
      /* While there are tokens in "string" */
      printf( " %s\n", token );
      /* Get next token: */
      token = strtok( NULL, seps );
   }
}
Output
Tokens:
 A
 string
 of
 tokens
 and
 some
 more
 tokens
(С) не Я

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)