First, indentation makes for much easier code reading.
Code:
#include <stdio.h>
int getandshort(char store[], char hold[]);
int nullit(char arr[]);
int main()
{
int i;
char storeline[100];
char holdspace[100];
nullit(storeline);
nullit(holdspace);
getandshort(storeline, holdspace);
for (i = 0; i < 100; ++i)
printf("%c", storeline[i]);
return 0;
}
int getandshort(char store[], char hold[])
{
int i;
int c;
for (i = 0; (c = getchar()) != '\n' && c != EOF; ++i) {
if (c != ' ' || c == ' ' && store[i - 1] != ' ')
store[i] = c;
}
store[i] = '\0';
}
int nullit(char arr[])
{
int i;
for (i = 0; i < 100; ++i) {
arr[i] = '\0';
}
}
Second, turn up the warnings.
Code:
$ gcc -Wall foo.c
foo.c: In function ‘getandshort’:
foo.c:25:30: warning: suggest parentheses around ‘&&’ within ‘||’ [-Wparentheses]
25 | if (c != ' ' || c == ' ' && store[i - 1] != ' ')
| ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
foo.c:29:1: warning: control reaches end of non-void function [-Wreturn-type]
29 | }
| ^
foo.c: In function ‘nullit’:
foo.c:37:1: warning: control reaches end of non-void function [-Wreturn-type]
37 | }
| ^
- both your functions could be declared void rather than int
- adding ( ) helps to make sense of your complicated logic.
The two problems are:
1. store[i-1] is off the end of the array when i = 0
2. You increment i for every character, not every valid character.