A
Alessio
Hi,
I'm wondering, there's a better way to convert a string into an array ?
Without using an additional pointer ?
//
#include <stdio.h>
//
#define MAX_WORDS 16
//
int main(void)
{
char text[] = "this is a string that I use for an experiment, that
should be work, I hope.";
char *p[MAX_WORDS] = { 0 };
int word_count = 0;
for ( int i = 0; '\0' != text; i++ )
{
if ( 0 == p[word_count] )
{
p[word_count] = &text;
}
if ( ' ' == text )
{
text = '\0';
if ( ++word_count >= MAX_WORDS )
{
printf("string has too many words!\n");
return -1;
}
}
}
++word_count;
for ( int i = 0; i < word_count; i++ )
{
printf("p[%d] = %s\n", i, p);
}
return 0;
}
Thanks.
I'm wondering, there's a better way to convert a string into an array ?
Without using an additional pointer ?
//
#include <stdio.h>
//
#define MAX_WORDS 16
//
int main(void)
{
char text[] = "this is a string that I use for an experiment, that
should be work, I hope.";
char *p[MAX_WORDS] = { 0 };
int word_count = 0;
for ( int i = 0; '\0' != text; i++ )
{
if ( 0 == p[word_count] )
{
p[word_count] = &text;
}
if ( ' ' == text )
{
text = '\0';
if ( ++word_count >= MAX_WORDS )
{
printf("string has too many words!\n");
return -1;
}
}
}
++word_count;
for ( int i = 0; i < word_count; i++ )
{
printf("p[%d] = %s\n", i, p);
}
return 0;
}
Thanks.