This code has a bunch of warnings. Unfortunately most C compilers do not issue warnings by default. Using -Wall turns on the most common warnings, though inexplicably not all of them.
That'll at least let you know that you're using printf incorrectly.
test.c:13:12: warning: incompatible integer to pointer conversion passing 'int' to parameter of type
'const char *' [-Wint-conversion]
printf(count);
^~~~~
printf takes a format string which tells it the type of the thing its turning into a string.
printf("%d\n", count);
The next problem is this for loop.
for (x = 1; x <= 1000; ++x) {
That counts from 1 to 1000. But arrays in C start at 0. For a 1000 element array, you want to go from 0 to 999 else you'll miss the first element and then walk off the end of the array.
for (x = 0; x < 1000; ++x) {
While myArray has been allocated memory on the stack, and its first few elements have been set to constant strings, everything after that will be NULL (if it were allocated with malloc they would be filled with garbage). Since you're iterating over all the elements you're passing null pointers to strlen which is causing a segfault. You have to stop at the first null. Putting a null pointer at the end of an array of pointers is a good way to know when to stop.
Finally, since myArray is full of pointers to constant strings, you should declare it a list of pointers to const char. That will quiet this warning that's telling you you might be trying to modify constant strings.
test.c:5:28: warning: initializing 'char *' with an expression of type 'const char [5]' discards
qualifiers [-Wincompatible-pointer-types-discards-qualifiers]
char *myArray[1000] = {"Oh! ", "Hello ", "world.", NULL};
Putting it all together...
#include <stdio.h>
#include <string.h>
int main() {
const char *myArray[1000] = {"Oh! ", "Hello ", "world."};
int count = 0, x;
for (x = 0; myArray[x] != NULL; ++x) {
count += strlen(myArray[x]);
}
printf("%d\n", count);
}
Now that we've got the basic loop working, we can replace strlen with something that's going to count the characters while skipping whitespace.
ctype.h has a bunch of functions for finding out what type a character is. We can use isspace to tell if a character is whitespace. But we're going to have to write our own function to loop through the string and examine all the characters.
There's a bunch of ways to loop through a string. We could get the length and iterate.
size_t len = strlen(string);
for( int i = 0; i < len; i++ ) {
...
}
But C doesn't store the length of a string. strlen has to do its own loop through the string until it sees a null byte, so really the above is looping through the string twice. We can do better and iterate through looking for the null byte ourselves.
Again, we could use a normal for loop and loop until we see a null byte.
for( int i = 0; string[i] != '\0'; i++ ) {
...
}
But we don't even need i. Instead, we can increment string directly, changing where the start of the string is, and always look at string[0].
int count_chars_without_spaces( const char *string ) {
int count = 0;
for( ; string[0] != '\0'; string++ ) {
if( !isspace(string[0]) ) {
count++;
}
}
return count;
}
That's ok because string is a variable local to that particular function. We can change it without changing the string.
So plug that in instead of strlen and you're good!
strlen. What have you tried? What problem are you having?