2

What this code does simply is to break up a sentence into individual word, for example: you input My name is John, it returns:

  1. My
  2. name
  3. is
  4. John

I'll like to know if there's any better way to rewrite this?

int main() {
    int w_size = 0;
    bool check_bool = false;
    char l_str[81];
    char *ptr_to_word[81];

    for (char *res_p = &(l_str[0]); *res_p != '\0'; res_p++) {
        if ((*res_p != '.') && (*res_p != ',') && (*res_p != ' ') && (check_bool == false)) {
            ptr_to_word[w_size] = res_p;
            w_size++;
            check_bool = true;
        }
        if (((*res_p == '.') || (*res_p == ',') || (*res_p == ' ')) && (check_bool == true)) {
            check_bool = false;
        }
    }

    if (w_size == 0) {
        printf("no solution");
    } else {
        for (int i = 0; i < w_size; i++) {
            char *a = ptr_to_word[i];
            while ((*a != ',') && (*a != '.') && (*a != '\0') && (*a != ' ')) {
                printf("%c", *a);
                a++;
            }
            printf("\n");
        }
    }
    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189

2 Answers2

1

the following proposed code:

  1. prompts the user for the sentence to be divided into words
  2. cleanly compiles
  3. performs the desired functionality

And now, the proposed code: (EDIT per chqrlie)

#include <stdio.h>
#include <string.h>

#define MAX_BUF_LEN 1024
#define MAX_WORDS 100


int main( void )
{
    char buffer[ MAX_BUF_LEN ] = {0};
    char *words[ MAX_WORDS ] = {NULL};

    printf( "%s\n", "Please enter a sentence to be divided into words" );

    if( fgets( buffer, sizeof( buffer ), stdin ) )
    {
        size_t wordCount = 0;
        char *token;

        token = strtok( buffer, ",. " );
        while( wordCount < MAX_WORDS && token )
        {
            words[ wordCount ] = token;
            wordCount++;
            token = strtok( NULL, ",. " );
        }

        for( size_t i = 0; i < wordCount; i++ )
        {
            printf( "%zu: %s\n\n", i+1, words[i] );
        }
    }
}

Here is the results of a typical run of the proposed code:

Please enter a sentence to be divided into words
This is a sentence to be divided into words
1: This

2: is

3: a

4: sentence

5: to

6: be

7: divided

8: into

9: words
user3629249
  • 16,402
  • 1
  • 16
  • 17
  • regarding: `for( size_t i = 0; words[i]; i++ )` the array of `words[]` is initialized to all NULL, so the second parameter in the `for()` statement will cause the loop to exit when it encounters an entry in `words[]` that contains NULL – user3629249 Apr 16 '20 at 21:58
  • Yes this works, but it excludes capital letters, any fix around that? and also it doesn't take input of punctuation marks as its skips them, is it possible to take all as input then just skips out when printing output? Thanks for the solution – Enterprevic Apr 17 '20 at 05:16
  • 1
    @chqrlieforyellowblockquotes, I wish you had put it that way to begin with. I'll modify my answer. – user3629249 Apr 17 '20 at 19:50
1

If you dont need to store the words into an array, you can output them directly:

#include <stdio.h>
#include <string.h>

int main() {
    char str[81];

    printf("Enter string: ");
    if (fgets(str, sizeof str, stdin)) {
        int pos = 0, len, index = 1;
        for (;;) {
            /* skip initial separators */
            pos += strspn(str + pos, ",.\n ");
            if (str[pos] == '\0')
                break;
            /* compute the length of the word */
            len = strcspn(str + pos, ",.\n ");
            printf("%d: %.*s\n", index++, len, str + pos);
            pos += len;
        }
    }
    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189