Question about a program that divides characters without using a function that divides strings such as strtok

Asked 2 years ago, Updated 2 years ago, 33 views

This is a question about the c language.
Declares the character array char text[] = "It is good to see you. Thank you for coming."; and
We are creating a program that divides text strings into words and outputs them as standard.

The rule does not print spaces or periods, and the words to be printed are '[' and ']'
one word per line. It is a program called
that is enclosed in .

You can also create words on the assumption that they are separated by a single character space.
A character string such as strtok is created without using a function for dividing it.Calling external programs is also not allowed

It is assumed that

Problems/Error Messages you are experiencing

 str[0]—It
str[1]—is
str[2]—[good]
str[3]—To
str[4]—See
str[5]—You.
str[6]—Thank
str[7]—You
str[8]—For
str [9]—[coming.]
Press any key to continue.

This is what you did when you ran the program.

However, please remove the space and period from the teacher.
I've tried various sites, but I can't get a reply. What should I do?
This is my first question, so I'm sure you're not familiar with it, but I appreciate your cooperation

int main (void)
{
    inti;
    char text[] = "It is good to see you. Thank you for coming.";
    char str[10][256+1];
    char*s,*d;

    s=text;
    for (i=0; i<10;i++)
    {
        d = str[i];

         while(*d++=*s++)!=_SPACE)
        {
        }
        *d = '\0';
    }

    for (i=0; i<10;i++)
    {
        printf("str[%d]:[%s]\n", i, str[i]);
    }
}

I looked at various sites and tried adding #define_SPACE0x20, but it didn't work.

c

2022-09-29 21:41

1 Answers

It is difficult to parse with a fixed length array.
Once you get used to it, variable-length data structures are easier.
Always be aware of out-of-range access when turning the pointer in a loop.
Then try to write a test (if you haven't done it, remember it).

#include<stdio.h>
# include<stdbool.h>
# include <string.h>
# include <cttype.h>
# include <assert.h>

// Let's not use magic numbers.
enum {
    SPACE=',
    PERIOD = '.'',
    TOKENS_SIZE=10,
    TOKEN_SIZE = 255,
};

// Let's name the data structure correctly.
static char tokens [TOKENS_SIZE] [TOKENS_SIZE+1];

/**
 * If the character is skipped, return true, otherwise false.
 */
static bool
is_skip_char(char){
    return ch==SPACE||ch==PERIOD;
}

/**
 * skip a character to be skipped
 */
static void
skip(const char**pt){
    for(;is_skip_char(**pt);++*pt){
    }
}

/**
 * Write a comment on the function.
 */
static void
split(const char*text){
    // Initialize state
    for(inti=0;i<TOKENS_SIZE;++i){
        tokenens[i][0]='\0';
    }

    const char*pt=text;

    skip(&pt);// skip the characters to skip at the beginning of the line

    // Be aware of access outside the pointer and write code.
    for(inti=0;i<TOKENS_SIZE&*pt;++i){
        char*tok=tokens[i];
        char*tokend=tok+TOKEN_SIZE;//Prevent out-of-range access

        for (;tok<tokend&*pt;){
            if(is_skip_char(*pt)){
                break;
            }

            *tok++=*pt++;
        }

        skip (&pt);
        *tok='\0';
    }
}

/**
 * Let's separate the analysis and the output.
 */
static void
show(void){
    for(inti=0;i<TOKENS_SIZE;++i){
        printf("tokens[%d]:[%s]\n", i,tokens[i]);
    }
}

/**
 * Let's write a test.
 */
static void
test(void){
    split("Hi Bob.Hi Michael";
    assert(!strcmp(tokens[0], "Hi"));
    assert(!strcmp(tokens[1], "Bob"));
    assert(!strcmp(tokens[2], "Hi"));
    assert(!strcmp(tokens[3], "Michael"));

    split("abc def..ghijkl");
    assert(!strcmp(tokens[0], "abc"));
    assert(!strcmp(tokens[1], "def"));
    assert(!strcmp(tokens[2], "ghi"));
    assert(!strcmp(tokens[3], "jkl"));

    split("abcd");
    assert(!strcmp(tokens[0], "abcd"));
    US>assert(!strcmp(tokens[1], ""));

    split(".abcd";
    assert(!strcmp(tokens[0], "abcd"));
    US>assert(!strcmp(tokens[1], ""));

    split("abcd.");
    assert(!strcmp(tokens[0], "abcd"));
    US>assert(!strcmp(tokens[1], ""));

    split("";
    US>assert(!strcmp(tokens[0], ""));
    US>assert(!strcmp(tokens[1], ""));

    split("...");
    US>assert(!strcmp(tokens[0], ""));
    US>assert(!strcmp(tokens[1], ""));

    split(".");
    US>assert(!strcmp(tokens[0], ""));
    US>assert(!strcmp(tokens[1], ""));

    split(".");
    US>assert(!strcmp(tokens[0], ""));
    US>assert(!strcmp(tokens[1], ""));
}

int
main(void){
    // Let's not write long codes for the main function.
    split("It is good to see you. Thank you for coming.");
    show();

    // Let's do a test.
    test();

    // Let's turn it back.
    return 0;
}


2022-09-29 21:41

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.