468,491 Members | 1,996 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,491 developers. It's quick & easy.

How split 1 array in two

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
Dec 2 '07 #1
7 3500
ri****************@gmail.com wrote:
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).
Dec 2 '07 #2
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 );
}
Dec 2 '07 #3
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 [i]

Can you give some tips / help.

Thanks.
Dec 2 '07 #4
ri****************@gmail.com wrote:
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.

--
Thad
Dec 2 '07 #5
ric wrote:
>
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 ----------*/

--
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>
Try the download section.

--
Posted via a free Usenet account from http://www.teranews.com

Dec 2 '07 #6
On Dec 2, 11:57 pm, ricardoramoscab...@gmail.com wrote:
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?
Dec 3 '07 #7
ric
James Fang escreveu:
On Dec 2, 11:57 pm, ricardoramoscab...@gmail.com wrote:
>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 :-(
Dec 8 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by nieuws | last post: by
1 post views Thread by jhcorey | last post: by
3 posts views Thread by Jan Hanssen | last post: by
4 posts views Thread by Craig Buchanan | last post: by
5 posts views Thread by kurt sune | last post: by
7 posts views Thread by lgbjr | last post: by
12 posts views Thread by garyusenet | last post: by
reply views Thread by NPC403 | last post: by
3 posts views Thread by gieforce | last post: by
reply views Thread by theflame83 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.