JavascriptLoser
JavascriptLoser

Reputation: 1962

Iterate through a character array while skipping over whitespace

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

Answers (4)

clearlight
clearlight

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

Dhiren Dash
Dhiren Dash

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

Sourav Ghosh
Sourav Ghosh

Reputation: 134286

My suggestion, go with strtok(). It'll be much easy.

The idea:

  1. Use strtok() and space ' ' as delimiter to find out each occurrence of words.
  2. use strcpy() to copy to copy the returned tokens (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.

  1. You can get rid of the for loop iteraing over the input string.
  2. You need to contuinue the strtok() on the same input untill it rerturns NULL.
  3. 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

Srinath Mandava
Srinath Mandava

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

Related Questions