简体   繁体   English

断开字符串指针直到c中的标记

[英]Breaking a string pointer till a marker in c

I have a pointer char * c = "I - need - to - do - this - break". 我有一个指针char * c =“我-需要-要做-这个-休息”。 I need to break it according to the '-' such that in each iteration I get an output as "I" then "I -need" then " I - need - to" and so on till the whole string. 我需要根据“-”将其断开,以便在每次迭代中得到的输出分别为“ I”,“ I -need”,“ I-need-to”,依此类推,直到整个字符串。 Any help? 有什么帮助吗?

The simple way is to make c mutable and not a pointer to a String Literal . 简单的方法是使c可变,而不是指向String Literal的指针。 That way all you need to do is work down the string (either using a pointer or an index) and keep track of whether you are in a word or not in a word. 这样,您所需要做的就是处理字符串(使用指针或索引)并跟踪您是否在单词中。 When you hit a space (or hyphen if you get rid of the spaces), if you are in a word, save the current char, overwrite the current char in the array with '\\0' to terminate the string at the current character and print it. 当您打一个空格时(如果要删除空格,则打连字符),如果您在单词中,请保存当前字符,用'\\0'覆盖数组中的当前字符以在当前字符处终止字符串,打印它。 Restore the current character in the array and repeat until you run out of characters, eg 恢复数组中的当前字符并重复执行,直到用完字符为止,例如

#include <stdio.h>

int main (void) {

    char c[] = "I - need - to - do - this - break", /* an array */
        *p = c;     /* pointer to current char */
    int in = 0;     /* flag for in/out of word */

    while (*p) {    /* loop over each char */
        if (*p == ' ' || *p == '-') {   /* if a space or hyphen */
            if (in) {                   /* if in a word */
                char current = *p;      /* save current char */
                *p = 0;                 /* nul-terminate array at current */
                puts (c);               /* print array */
                *p = current;           /* restore current in array */
                in = 0;                 /* set flag out of word */
            }
        }
        else {          /* otherwise, not a space or hyphen */
            in = 1;     /* set flag in word */
        }
        p++;            /* advance to next char */
    }

    if (in)             /* if in word when last char reached */
        puts (c);       /* output full string */
}

Example Use/Output 使用/输出示例

$ ./bin/incremental
I
I - need
I - need - to
I - need - to - do
I - need - to - do - this
I - need - to - do - this - break

Using a Non-Mutable String Literal 使用非可变字符串文字

If you must use a non-mutable String Literal , then the approach is largely the same. 如果必须使用不可更改的String Literal ,则方法基本相同。 The only difference being you cannot nul-terminate the original string, so you are left using another pointer (or index) to output each character from the beginning until you reach the current using putchar (or getting the number of characters from p - c and then copying to a buffer to nul-terminate and output all at once). 唯一的区别是您不能对原始字符串进行n终止,因此您将使用另一个指针(或索引)从头开始输出每个字符,直到使用putchar到达当前字符为止(或者从p - c和然后复制到缓冲区以nul终止并立即全部输出)。 Simply looping until you reach the current and using putchar for output is about as easy as anything else, eg 简单地循环直到达到电流并使用putchar进行输出几乎和其他任何事情一样容易,例如

#include <stdio.h>

int main (void) {

    char *c = "I - need - to - do - this - break",  /* string literal */
        *p = c;     /* pointer to current char */
    int in = 0;     /* flag for in/out of word */

    while (*p) {    /* loop over each char */
        if (*p == ' ' || *p == '-') {   /* if a space or hypen */
            if (in) {                   /* if in a word */
                char *wp = c;           /* get pointer to start */
                while (wp < p)          /* loop until you reach current */
                    putchar (*wp++);    /* output each char & increment */
                putchar ('\n');         /* tidy up with newline */
                in = 0;                 /* set flag out of word */
            }
        }
        else {          /* otherwise, not a space or hyphen */
            in = 1;     /* set flag in word */
        }
        p++;            /* advance to next char */
    }

    if (in)             /* if in word when last char reached */
        puts (c);       /* output full string */
}

(the output is the same) (输出是相同的)

Look things over and let me know if you have questions. 仔细检查一下,如果您有任何问题,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM