How split 1 array in two

  • Thread starter ricardoramoscabral
  • Start date
R

ricardoramoscabral

Hello to All.


I have this array of strings: char str[90] = {" comp " Hello World
" "};

And i whant to split in two arrays from the original: ( { <-----
str1 -------> "<---str2 -------->" }

str1[10] = {comp}
str2[80] = {Hello World}

sorry for my english
 
M

Martin Ambuhl

Hello to All.


I have this array of strings: char str[90] = {" comp " Hello World
" "};

char str[90] /* = { ... } */; is _not_ an array of strings. It is an
array of char, which can contain a string,
Your initializer has an odd number of '"' characters. Apart from the
embedded end-of-line, it is for that reason malformed.

And i whant to split in two arrays from the original: ( { <-----
str1 -------> "<---str2 -------->" }

str1[10] = {comp}
str2[80] = {Hello World}

You might use strtok, but your current level of confusion suggests that
you would not do so in anything like safety. Just find the beginning
and end of each substring (functions like strstr, strchr, strlen can be
useful) and copy them into your new strings with functions likw memmove,
memcpy, or strncpy (strcpy is unlikely to handle any but the last the
way you expect).
 
R

ricardoramoscabral

int i=0;
char str[] = "portugal\"a-programar";
char delims[] = "\"";
char *result = NULL;
result = strtok( str, delims );
while( result != NULL )
{
printf( "result is \"%s\"\n", result );
result = strtok( NULL, delims );
}
 
R

ric

Hello Martin Ambuhl,


With strtok works.

int i=0;
char str[] = "Teste\"a-programar";
char delims[] = "\"";
char *result = NULL;
result = strtok( str, delims );
while( result != NULL )
{
printf( "result is \"%s\"\n", result );
result = strtok( NULL, delims );
}


I am trying to save the result of strtok in several separate arrays, but
I am not succeed.

We tried downloading string, using "if" with counter

Can you give some tips / help.

Thanks.
 
T

Thad Smith

I have this array of strings: char str[90] = {" comp " Hello World
" "};

That is not a valid statement in C and I don't know what you intended.
Please post again with a valid declaration. char str[90] declares an
array of char in C, not an array of strings.
And i whant to split in two arrays from the original: ( { <-----
str1 -------> "<---str2 -------->" }

str1[10] = {comp}
str2[80] = {Hello World}

Those are not valid C declarations, either. Clarify your problem.
sorry for my english

Your English is understandable.
 
C

CBFalconer

ric said:
With strtok works.
.... snip ...

Can you give some tips / help.

Try the following tknsplit routine. It is different from strtok,
but I believe cleaner to use. For general use, compile it without
defining TESTING.

/* ------- file tknsplit.c ----------*/
#include "tknsplit.h"

/* copy over the next tkn from an input string, after
skipping leading blanks (or other whitespace?). The
tkn is terminated by the first appearance of tknchar,
or by the end of the source string.

The caller must supply sufficient space in tkn to
receive any tkn, Otherwise tkns will be truncated.

Returns: a pointer past the terminating tknchar.

This will happily return an infinity of empty tkns if
called with src pointing to the end of a string. Tokens
will never include a copy of tknchar.

A better name would be "strtkn", except that is reserved
for the system namespace. Change to that at your risk.

released to Public Domain, by C.B. Falconer.
Published 2006-02-20. Attribution appreciated.
Revised 2006-06-13 2007-05-26 (name)
*/

const char *tknsplit(const char *src, /* Source of tkns */
char tknchar, /* tkn delimiting char */
char *tkn, /* receiver of parsed tkn */
size_t lgh) /* length tkn can receive */
/* not including final '\0' */
{
if (src) {
while (' ' == *src) src++;

while (*src && (tknchar != *src)) {
if (lgh) {
*tkn++ = *src;
--lgh;
}
src++;
}
if (*src && (tknchar == *src)) src++;
}
*tkn = '\0';
return src;
} /* tknsplit */

#ifdef TESTING
#include <stdio.h>

#define ABRsize 6 /* length of acceptable tkn abbreviations */

/* ---------------- */

static void showtkn(int i, char *tok)
{
putchar(i + '1'); putchar(':');
puts(tok);
} /* showtkn */

/* ---------------- */

int main(void)
{
char teststring[] = "This is a test, ,, abbrev, more";

const char *t, *s = teststring;
int i;
char tkn[ABRsize + 1];

puts(teststring);
t = s;
for (i = 0; i < 4; i++) {
t = tknsplit(t, ',', tkn, ABRsize);
showtkn(i, tkn);
}

puts("\nHow to detect 'no more tkns' while truncating");
t = s; i = 0;
while (*t) {
t = tknsplit(t, ',', tkn, 3);
showtkn(i, tkn);
i++;
}

puts("\nUsing blanks as tkn delimiters");
t = s; i = 0;
while (*t) {
t = tknsplit(t, ' ', tkn, ABRsize);
showtkn(i, tkn);
i++;
}
return 0;
} /* main */

#endif
/* ------- end file tknsplit.c ----------*/


/* ------- file tknsplit.h ----------*/
#ifndef H_tknsplit_h
# define H_tknsplit_h

# ifdef __cplusplus
extern "C" {
# endif

#include <stddef.h>

/* copy over the next tkn from an input string, after
skipping leading blanks (or other whitespace?). The
tkn is terminated by the first appearance of tknchar,
or by the end of the source string.

The caller must supply sufficient space in tkn to
receive any tkn, Otherwise tkns will be truncated.

Returns: a pointer past the terminating tknchar.

This will happily return an infinity of empty tkns if
called with src pointing to the end of a string. Tokens
will never include a copy of tknchar.

released to Public Domain, by C.B. Falconer.
Published 2006-02-20. Attribution appreciated.
revised 2007-05-26 (name)
*/

const char *tknsplit(const char *src, /* Source of tkns */
char tknchar, /* tkn delimiting char */
char *tkn, /* receiver of parsed tkn */
size_t lgh); /* length tkn can receive */
/* not including final '\0' */

# ifdef __cplusplus
}
# endif
#endif
/* ------- end file tknsplit.h ----------*/
 
J

James Fang

Hello to All.

I have this array of strings: char str[90] = {" comp " Hello World
" "};

And i whant to split in two arrays from the original: ( { <-----
str1 -------> "<---str2 -------->" }

str1[10] = {comp}
str2[80] = {Hello World}

sorry for my english

Is pointer enough for your split?
 
R

ric

James Fang escreveu:
Hello to All.

I have this array of strings: char str[90] = {" comp " Hello World
" "};

And i whant to split in two arrays from the original: ( { <-----
str1 -------> "<---str2 -------->" }

str1[10] = {comp}
str2[80] = {Hello World}

sorry for my english

Is pointer enough for your split?


I have pointer error :-(
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top