Reputation: 1962
I'm trying to write a program that iterates through a character array (which contains a sentence), and assigns the words in that sentence to another array, skipping over spaces (which is how we know separate words), and then prints those words.
Say for example my character array is:
{'t','h','e',' ','f','o','x',' ','r','u','n','s','\0',}
I want my function to assign the words to an array, call it words
, one at a time, skipping over the spaces, the words then printed one by one.
I tried this:
for (i = 0, x = 0; a[i] != '\0'; i++, x++){
if (a[i] != ' '){
words[x] = a[i];
}
else{
words[x] = '\0';
printf("%s\n", words);
x=0;
i++;
continue;
}
}
printf("%s\n", words);
But when I ran it, it only printed out s
Upvotes: 1
Views: 2628
Reputation: 12615
Note that the variables (except for the array a
, which is explicitly set to a string) defined outside of main are zeroed by default by the compiler and don't need to be explicitly initialized, but you must be very careful about initialization in general and especially within functions.
And a
, though defined as a pointer to a string literal can also be accessed as an array of characters.
Output:
words[0] = the
words[1] = fox
words[2] = runs
This works:
#include <stdio.h>
#define MAX_WORDS 1000
#define MAX_WORD_LEN 32
char *a = "the fox runs";
char words[MAX_WORDS][MAX_WORD_LEN];
int i, j, k;
int
main(void)
{
for (i = 0; k < MAX_WORDS && a[i] != '\0'; i++) {
if (a[i] != ' ' && j <= 32) {
words[k][j++] = a[i];
} else {
words[k++][j] = '\0';
printf("words[%d] = %s\n", k - 1, words[k - 1]);
j = 0;
}
}
printf("words[%d] = %s\n", k, words[k]);
}
Upvotes: 0
Reputation: 111
Is this the full program?
words[x] = a[i];
You would probably want to put words[x]
in a loop incrementing x
by 1
everytime you don' t find a whitespace.
Ok I noticied you have edited it. Also, its an array so I suppose you need to loop through the array and print each character.
For printing,
for (j = 0; j < sizeof(words)/sizeof(words[0]); j++ )
{
printf("%c", words[j] );
}
Upvotes: 0
Reputation: 134286
My suggestion, go with strtok()
. It'll be much easy.
The idea:
strtok()
and space ' '
as delimiter to find out each occurrence of words.strcpy()
to copy to copy the returned token
s (words) to the word
array.Note:
I want my function to assign the words to an array, call it words, one at a time, skipping over the spaces, the words then printed one by one.
Your current code does not look like it's doing the same.
for
loop iteraing over the input string.strtok()
on the same input untill it rerturns NULL
.words[x]
needs to be a char *
(with properly allocated memory) or char []
(array) to hold each word found from the input string and later to print one by one.Upvotes: 1
Reputation: 3462
With continue in for loop you don't need to have i++
and you need to set x=-1
.
for (i = 0, j = 0; a[i] != '\0'; i++, x++){
if (a[i] != ' '){
words[x] = a[i]; // U need to use j
}
else{
words[x] = '\0';
printf("%s\n", words);
x=-1; // as you are using continue
// i++; You don't need this
continue;
}
}
printf("%s\n", words);
Upvotes: 0