Storing string input into different arrays in C

Hello this brand new noob on C is trying to take a string input of 3 words and store it on 3 different arrays, not 2D nor 3D arrays. For this problem I'm not allow to use any of the string library functions. Basically I'm trying to implement the sscanf function. I created a function that breaks the input into the three words, and stores them in their indicated array. When I try to print the each of the arrays for my second array I can't get it to print the word I tried to store in that second array. Here it's what I have...

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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#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=0;

 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;
}

/*
 This function takes a line and breaks it into words.
 The orginal line is in the char array str, the first word
 will go into the char array c, the second into p1, and the
 the third into p2.  If there are no words, the corresponding
 char arrays are empty.  At the end, n contains the number of
 words read.
 */
void breakUp(char *str, char *c, char *p1, char *p2, int* n)
{
    c[0] = p1[0] = p2[0] = '\0';
    int j = 0; // str array index
    int i = 0; // index of rest of the arrays
    n[0] = 0;
    
    // stores first word in array c
    while(str[j]!= ' '|| str[j] == '\0')
      {
          c[i]= str[j];
          i++;
          j++;
      }
    // increases n count, moves j into next element
    // and sets i back to index 0
    if (str[j] == ' '|| str[j] == '\0')
      {
         c[i] = '\0';
          n[0]++;
          j++;
          i =0;
          
      }
    
    // stores second word in array p1
    while(str[j]!= ' '|| str[j] == '\0')
    {
        p1[i]= str[j];
        i++;
        j++;
    }
    // increases n count, moves j into next element
    // and sets i back to index 0
    if (str[j] == ' '|| str[j] == '\0')
    {
        p1[i] = '\0';
        n[0]++;
        j++;
        i =0;
        
    }
    
    // stores 3rd word in array p2
    while(str[j] != ' ' || str[j] == '\0')
      {
          p2[i] = str[j];
          i++;
          j++;
      }
    // increases n count, moves j into next element
    // and sets i back to index 0
    if(str[j] == ' ' || str[j] == '\0')
      {
          p2[i] = '\0';
          n[0]++;
	  }

	
}


Advanced thanks if any help is provided
Last edited on
It prints all three words for me (using x64 tdm-gcc compiler), but it throws an exception too.

I think it is all done quite well, but you made a little mistake with incrementing the n in the breakUp function.

n is a parameter of type "pointer to an integer", but you seem to be treating it as an array. For me, when I replaced "n[0]++" with "*n++" (increment the value that n points to) the crash disappeared.

I hope that helps you a bit.

Kind regards, Nico
which is (n++)[0] if you prefer array notation.
I stopped using * notation because I mostly worked with math, and *d * *a is a little nerve-wracking.
Topic archived. No new replies allowed.