473,241 Members | 1,658 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,241 software developers and data experts.

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 3894
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: nieuws | last post by:
Hi, I was trying to do the following. It's my first php "project", so it's quiet logic that i have some problems. Perhaps the php community might help. It's about this : I have a txt file...
1
by: jhcorey | last post by:
I don't know where the actual issue is, but hopefully someone can explain. The following displays "5" in FireFox, but "3" in IE: <script type="text/javascript" language="javascript"> var...
3
by: Jan Hanssen | last post by:
Hi! I have a list of data in a textfile which is tab delimited. Each line is seperated by a VbCrLf. I want to collect this data in a multidimensional string array. I do not wish to use a...
4
by: Craig Buchanan | last post by:
I am trying to split a comma-delimited string into a string array. unfortunately, if the string doesn't contain a comma, the resulting array is Nothing. other than using vb6 compatibility, is...
3
by: Ben | last post by:
Hi I am creating a dynamic function to return a two dimensional array from a delimeted string. The delimited string is like: field1...field2...field3... field1...field2...field3......
5
by: kurt sune | last post by:
The code: Dim aLine As String = "cat" & vbNewLine & "dog" & vbNewLine & "fox" & vbNewLine Dim csvColumns1 As String() = aLine.Split(vbNewLine, vbCr, vbLf) Dim csvColumns2 As String() =...
7
by: lgbjr | last post by:
Hi All, I'm trying to split a string on every character. The string happens to be a representation of a hex number. So, my regex expression is (). Seems simple, but for some reason, I'm not...
2
by: Digital Fart | last post by:
following code would split a string "a != b" into 2 strings "a" and "b". but is there a way to know what seperator was used? string charSeparators = { "=", ">=", "<=" , "!=" }; string s1 =...
12
by: garyusenet | last post by:
string lines = File.ReadAllLines(@"c:\text\history.txt"); foreach (string s in lines) { ArrayList results = new ArrayList(); string delimit = ";"; string currentline = s.Split(";"); ...
14
by: Stevo | last post by:
If you split a string into an array using the split method, it's not working the way I'd expect it to. That doesn't mean it's wrong of course, but would anyone else agree it's working somewhat...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.