Reading a string and store it in multiple arrays in C

Hello I'm new to C and I'm trying to take a string input that takes up to 3 words, and store it in one array, and then store every word from that input in another 3 different arrays, and return the number of inputs. Here it's what I have:(note, I don't want to use 2D or 3D arrays)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include<stdio.h>
#include<string.h>


void breakUp(char *, char *, char *, char *, int );
int main()
{
 char line[80], comm[10], p1[10], p2[10];
 int len, n;

 printf("Please Enter a command: ");

 fgets(line, 80, stdin);
    /* get rid of trailing newline character */
    len = strlen(line) - 1;
    if (line[len] == '\n')
        line[len] = '\0';
    
    /* Break up the line */
    breakUp(line, comm, p1, p2, &n);
    
    printf ("%d things on this line\n", n);
    printf ("command: %s\n", comm);
    printf ("parameter 1: %s\n", p1);
    printf ("parameter 2: %s\n", p2);
    
    return 0;
}

void breakUp(char *str, char *c, char *p1, char *p2, int n)
{
    c[0] = p1[0] = p2[0] = '\0';
    int j = 0;
    int i = 0;
    
    while(str[j]!= ' ')
      {
	c[i]= str[j];
	i++;
	j++;
      }
    if (str[j] == ' ')
      {
	n++;
	j++;
	i =0;
      }
    while(str[j] != ' ')
      {
	p1[i]= str[j];
	j++;
	i++;
      }
    if(str[j] == ' ')
      {
	n++;
	j++;
	i = 0;
      }
    while(str[j] != ' ')
      {
	p2[i] = str[j];
	j++;
	i++;
      }
    if(str[j] == ' ' || str[j] == '\n')
      {
	n++;
	  }

}
Last edited on
What is the problem ?
\n isn't always 1 char; it is 2 on windows... use 13 or 10 ascii value maybe.
should be fairly straightforward:


strcpy (worker, all3input);
int x = strlen(worker);
for(j = 0; j< x; j++)
if(worker[j] == ' ')
worker[j] = 0;

strcpy(res1, worker);
worker += strlen(res1);
strcpy(res2, worker);
worker += strlen(res2);
strcpy(res3, worker);

something like that. You will need worker to be a pointer into a string. You will have to double check my pointer math, I could be off by one as I typed that in a hurry. But the algorithm/approach should do it.

if they put in more than 3, it was unclear what you wanted to do.
Last edited on
For this problem I'm not allowed to use any of the string functions, the problem right now is that I can't return the number of words entered to my 'n' variable, and when I print the words I can't get the second word to show up.
> \n isn't always 1 char; it is 2 on windows...

In every C and C++ implementations, '\n' is required to be a single (unique) character.
Though it may be represented externally, outside the program (for example in text based internet protocols like HTTP or in a file system) as some implementation specific sequence of two or more bytes.

The C programming language provides the escape sequences '\n' (newline) and '\r' (carriage return). ... The C standard only guarantees two things:

1. Each of these escape sequences maps to a unique implementation-defined number that can be stored in a single char value.
...
https://en.wikipedia.org/wiki/Newline#In_programming_languages


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>

// extract the next token in srce into token.
// return the number of characters processed in srce
// invariant: non-NULL pointers
size_t extract_next_token( const char* srce, char* token )
{
    size_t n = 0 ;

    while( *srce && isspace(*srce) ) { ++srce ; ++n ; } // skip leading white space
    for( ; *srce && !isspace(*srce) ; ++n ) *token++ = *srce++ ; // copy non-ws characters
    *token = 0 ; // null terminate

    return n ; // number of characters processed
}

// extract up to a maximum of sz tokens. return number of tokens extracted
// invariant: non-NULL top-level and non-top-level pointers
size_t extract_tokens( const char* srce, char* tokens[], size_t sz )
{
    for( size_t i = 0 ; i < sz ; ++i )
    {
        size_t offset = extract_next_token( srce, tokens[i] ) ;
        if( tokens[i][0] == 0 ) return i ; // return number of tokens extracted
        srce += offset ;
    }

    return sz ;
}

// extract up to three tokens into a, b and c. return number of tokens extracted
// invariant: non-NULL pointers
size_t break_up( const char* srce, char* token_a, char* token_b, char* token_c )
{
    char* tokens[3] = { token_a, token_b, token_c } ;
    return extract_tokens( srce, tokens, 3 ) ;
}

int main()
{
    const char text[] = "   C is quirky, flawed,  \t  and an enormous success.\n"
                        "       - Dennis M. Ritchie.\n         " ;

    enum { MAX_TOK_SZ = 128, NUM_TOKS = 16 };
    char tokens[NUM_TOKS][MAX_TOK_SZ] ;

    char* tok_ptrs[NUM_TOKS] ;
    for( size_t i = 0 ; i < NUM_TOKS ; ++i ) tok_ptrs[i] = tokens[i] ;
    size_t n = extract_tokens( text, tok_ptrs, NUM_TOKS ) ;
    for( size_t i = 0 ; i < n ; ++i ) printf( "%s ", tokens[i] ) ;

    char a[MAX_TOK_SZ], b[MAX_TOK_SZ], c[MAX_TOK_SZ] ;
    n = break_up( text, a, b, c ) ;
    assert( n == 3 ) ;
    printf( "\n\n1. %s\n2. %s\n3. %s\n", a, b, c ) ;
}

http://coliru.stacked-crooked.com/a/6acfcd21e6c0ac15
Topic archived. No new replies allowed.