主頁 > 移動端開發 > C文本格式化程式字符計數已關閉

C文本格式化程式字符計數已關閉

2022-05-14 14:22:57 移動端開發

所以我有一個根據規則格式化文本的任務,并最終列印出字串中的字符數(包括\n和空格,除了\0字串末尾的任何內容)。

基本上,一個有效的輸入以一個點結束,但我認為在最后一個點之后我還有一些空格。我嘗試了幾種方法,例如\0從字串末尾開始替換空格的回圈。似乎沒有任何作業...

編輯要求是:

  1. 將雙點(..)轉換為新行
  2. 洗掉多個空格,只留下一個,
  3. 確保逗號或點之前沒有空格
  4. 確保逗號或點后有一個空格。
  5. 不改變撇號之間的原始內容。
  6. 并驗證僅在正確的位置(新行\段落)有大寫字母。

我們需要在 main 函式中完成所有代碼(不幸的是),而我的代碼通常會將字符數誤認為是 1-2 額外的計數(可能會在最后一個點之后做額外的空格

這是我的代碼在計數字符時失敗的輸入示例

the LANGUAGE  "C" is a procedural              programming language     .It was initially developed by "Dennis Ritchie"..            the Main feAtures of "C" language include low-level access to memory, simple set of keywords, and clean style                .
int main() {
    char ans;

    printf("*************** Welcome to the text cleaner ***************\n\n");

    do
    {
        int length, i, j = 0;
        int word, sentence, para, space;
        char tin[601], tout[601], * dex, * pos;
        printf("\nPlease enter text:\n");
        gets_s(tin, 600);
        length = strlen(tin);

        dex = strchr(tin, '.'); //converts double dots to new line
        while (dex != NULL)
        {
            if (tin[dex - tin   1] == '.') {
                tin[dex - tin   1] = '\n';
            }
            dex = strchr(dex   1, '.');
        }
        length = strlen(tin);


        dex = strchr(tin, ' '); //converting multiple spaces to single space
        while (dex != NULL)
        {
            while (dex != NULL && tin[dex - tin   1] == ' ')
            {
                for (i = dex - tin   1; i < strlen(tin); i  )
                {
                    tin[i - 1] = tin[i];
                }
                dex = strchr(dex, ' ');
                j  ;
            }
            dex = strchr(dex   1, ' ');
        }

        tin[length - j] = '\0';
        j = 0;

        dex = strchr(tin, '\n');

        while (dex != NULL && tin[dex-tin 1] == ' ') //delets spaces in the beggining of new row
        {
            for (i = dex - tin   1;i < strlen(tin);i  ) {
                tin[i] = tin[i   1];
            }
            dex = strchr(dex   1, '\n');
        }

        dex = strchr(tin, ','); //deletes space before comma
        while (dex != NULL && tin[dex - tin - 1] == ' ')
        {
            for (i = dex - tin - 1; i < strlen(tin); i  )
            {
                tin[i] = tin[i 1];
            }
            dex = strchr(dex 1, ',');
        }

        dex = strchr(tin, '.'); //deletes space before dots
        while (dex != NULL && tin[dex - tin - 1] == ' ')
        {
            for (i = dex - tin - 1; i < strlen(tin); i  )
            {
                tin[i] = tin[i 1];
            }
            dex = strchr(dex   1, '.');
        }



        dex = strchr(tin, ','); // adds space after comma
        while (dex != NULL && tin[dex - tin   1] != ' ')    
        {
            if (tin[dex - tin   1] != '\n')
            {
                tin[strlen(tin)   1] = '\0';
                for (i = strlen(tin); i > dex - tin; i--)
                {
                    if (i == dex - tin   1)
                    {
                        tin[i] = ' ';
                    }
                    else
                    {
                        tin[i] = tin[i - 1];
                    }
                }
                dex = strchr(dex   1, ',');
            }
        }

        dex = strchr(tin, '.'); // adds space after dot

        while (dex != NULL && tin[dex - tin   1] != ' ')
        {
                tin[strlen(tin)   1] = '\0';
                if (tin[dex - tin   1] == '\n')
                {
                    dex = strchr(dex   1, '.');
                }
                else
                {
                    for (i = strlen(tin); i > dex - tin; i--)
                    {
                        if (i == dex - tin   1)
                        {
                            tin[i] = ' ';
                        }
                        else
                        {
                            tin[i] = tin[i - 1];
                        }
                    }
                    dex = strchr(dex   1, '.');
                }           
        }

        strcpy_s(tout, sizeof(tout), tin);
        _strlwr_s(tout,sizeof(tout)); //copies and lowercasing the input string

        dex = strchr(tin, '"');
        
        if (dex != NULL) {
            pos = strchr(dex   1, '"');

            while (dex != NULL)
            {
                for (i = dex - tin; i < pos - tin; i  ) {
                    tout[i] = tin[i];
                }
                dex = strchr(pos   1, '"');
                if (dex)
                {
                    pos = strchr(dex   1, '"');
                }
            }  //making sure that the letters in the quotes have't been lowercased
        }
        
        _strupr_s(tin, sizeof(tin));

        dex = strchr(tout, '.');
        pos = strchr(tin, '.');
        while (dex != NULL && pos != NULL)
        {
                tout[dex - tout   2] = tin[pos - tin   2];
                dex = strchr(dex   1, '.');     
                pos = strchr(pos   1, '.');
        }   
        //CAPSLOCK

        dex = strchr(tout, '.'); //deletes space before dots
        while (dex != NULL)
        {
            if (tout[dex - tout - 1] == ' ')
            {
                for (i = dex - tout - 1; i < strlen(tout); i  )
                {
                    tout[i] = tout[i 1];
                }
            }
            
            dex = strchr(dex   1, '.');
        }

        if (tout[0] == ' ') {
            for (i = 0 ;i < strlen(tout); i  ) {
                tout[i] = tout[i   1];
            }
        }//handeling single space in the beggining of the string

        if (tout[0] >= 'a' && tout[0] <= 'z') {
            tout[0] -= 32;
        } //First letter always capital


        word = 0;
        sentence = 0;
        para = 1;
        space = 0;
        length = strlen(tout);

        for (i = 0; tout[i] != '\0';i  )
        {
            if (tout[i] == ' ' && tout[i   1] != ' ')
                word  ;
        }



        dex = strchr(tout, '.');
        while (dex != NULL)
        {
            sentence  ;
            dex = strchr(dex   1, '.');
        }

        dex = strchr(tout, '\n');
        while (dex != NULL)
        {
            space  ;
            para  ;
            word  ;
            dex = strchr(dex   1, '\n');
        } 

        //dex = strchr(tout, '-');
        //while (dex != NULL)
        //{
        //  word  ;
        //  dex = strchr(dex   1, '-');
        //}

        printf_s("\nText after cleaning:\n------------------------------------------------------------------------------------------------\n");
        printf_s("%s\n\n", tout);
        printf_s("characters: %d | words: %d | sentences: %d | paragraphs: %d\n------------------------------------------------------------------------------------------------\n",length, word, sentence, para);
        printf_s("\nIf you want to clean another string press (y): ");
        scanf_s(" %c", &ans, 1);
        if (ans == 'y')
        {
            gets_s(tin, 600);

        }
    } while (ans =='y');

uj5u.com熱心網友回復:

正如我在頂部評論中提到的,這可以在帶有狀態變數的單個回圈中完成。

幾個假設:

  1. 每當我們看到..(轉換為換行符)時,它都會開始一個新段落
  2. 你所說的“撇號”,我稱之為雙引號(因為這是唯一有意義的事情)。
  3. 在引號內沒有任何轉換
  4. 引號 [它們自己] 被復制(即被剝離)

不幸的是,我不得不完全重構代碼。它是注釋的。我意識到你必須使用main. 額外的功能僅用于除錯,因此它們“不算數”:

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

int opt_d;                          // debug
int opt_quo;                        // preserve quote

#if DEBUG
#define dbgprt(_fmt...) \
    do { \
        if (opt_d) \
            printf(_fmt); \
    } while (0)
#else
#define dbgprt(_fmt...)         do { } while (0)
#endif

#define COPYX(_chr) \
    do { \
        *dst   = _chr; \
        dbgprt("COPY %2.2X/%s\n",_chr,showchr(_chr)); \
    } while (0)

#define COPY \
    COPYX(chr)

#define WHITEOUT \
    do { \
        if (! white) \
            break; \
        COPYX(' '); \
        white = 0; \
        ctr_word  = 1; \
    } while (0)

const char *
showchr(int chr)
{
    static char buf[10];

    if ((chr >= 0x20) && (chr <= 0x7E))
        sprintf(buf,"%c",chr);
    else
        sprintf(buf,"{%2.2X}",chr);

    return buf;
}

void
showbuf(const char *buf,const char *who)
{
    const char *sep = "'";

    printf("%s: %s",who,sep);

    for (int chr = *buf  ;  chr != 0;  chr = *buf  )
        printf("%s",showchr(chr));

    printf("%s\n",sep);
}

int
main(int argc,char **argv)
{
    char inp[1000];
    char buf[1000];
    char *src;
    char *dst;

    --argc;
      argv;

    for (;  argc > 0;  --argc,   argv) {
        char *cp = *argv;
        if (*cp != '-')
            break;

        cp  = 2;
        switch (cp[-1]) {
        case 'd':
            opt_d = ! opt_d;
            break;
        case 'q':
            opt_quo = ! opt_quo;
            break;
        }
    }

    opt_quo = ! opt_quo;

    const char *file;
    if (argc > 0)
        file = *argv;
    else
        file = "inp.txt";

    FILE *xfsrc = fopen(file,"r");
    if (xfsrc == NULL) {
        perror(file);
        exit(1);
    }

    while (fgets(inp,sizeof(inp),xfsrc) != NULL) {
        strcpy(buf,inp);

        src = buf;
        dst = buf;

        int quo = 0;
        int white = 0;
        int dot = 1;
        int ctr_sent = 0;
        int ctr_word = 0;
        int ctr_para = 1;

        for (int chr = *src  ;  chr != 0;  chr = *src  ) {
            dbgprt("LOOP %2.2X/%s quo=%d white=%d dot=%d word=%d sent=%d para=%d\n",
                chr,showchr(chr),quo,white,dot,
                ctr_word,ctr_sent,ctr_para);

            // got a quote
            if (chr == '"') {
                if (! quo)
                    WHITEOUT;
                if (opt_quo)
                    COPY;
                quo = ! quo;
                continue;
            }

            // non-quote
            else {
                if (quo) {
                    COPY;
                    continue;
                }
            }

            // got a dot
            if (chr == '.') {
                dot = 1;

                // double dot --> newline (new paragraph)
                if (*src == '.') {
                    COPYX('\n');
                      src;
                    ctr_para  = 1;
                    continue;
                }

                COPY;

                white = 1;
                continue;
            }

            // from fgets, this can _only_ occur at the end of the buffer
            if (chr == '\n') {
                dot = 1;
                white = 1;
                COPY;
                break;
            }

            // accumulate/skip over whitespace
            if (chr == ' ') {
                white = 1;
                continue;
            }

            // output accumulated whitespace
            WHITEOUT;

            // got uppercase -- convert to lowercase if we're not at the start
            // of a sentence
            if (isupper(chr)) {
                if (! dot)
                    chr = tolower(chr);
            }

            // got lowercase -- capitalize if we're just starting a sentence
            else {
                if (islower(chr)) {
                    if (dot)
                        chr = toupper(chr);
                }
            }

            COPY;

            // count sentences
            if (dot)
                ctr_sent  = 1;

            dot = 0;
        }

        *dst = 0;

        showbuf(inp,"inp");
        showbuf(buf,"buf");

#if 0
        if (dot)
            ctr_word  = 1;
#endif

        printf("TOTAL: length=%zu sentences=%d paragraphs=%d words=%d\n",
            strlen(buf),ctr_sent,ctr_para,ctr_word);
    }

    fclose(xfsrc);

    return 0;
}

這是程式輸出:

inp: 'the LANGUAGE  "C" is a procedural              programming language     .It was initially developed by "Dennis Ritchie"..            the Main feAtures of "C" language include low-level access to memory, simple set of keywords, and clean style                .{0A}'
buf: 'The language "C" is a procedural programming language. It was initially developed by "Dennis Ritchie"{0A} The main features of "C" language include low-level access to memory, simple set of keywords, and clean style.{0A}'
TOTAL: length=214 sentences=3 paragraphs=2 words=31

更新:

太好了謝謝!我已經采取了你所做的并在我的代碼上實作了它,基本上,教授將我們限制在 stdio.h 和 string.h 庫中,所以我不能使用任何其他函式...... – Nitai Dan

別客氣!

我很高興您能夠將我的代碼合并到您的代碼中。這是所有可能的學習場景中最好的。

我不確定我是否添加了足夠的注釋以使我的演算法清晰,所以我對其進行了一些清理,并在考慮是否發布它。它張貼在下面。

根據您剛才所說,我 [仍然] 不確定是否允許創建您自己的函式。正如我所說,如果除錯函式不改變演算法,它們 [可能] 就可以了。

無論如何,這是更新的代碼:

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

int opt_d;                          // debug
int opt_quo;                        // preserve quote
int opt_x;                          // show space as hex

#define _dbgprt(_fmt...) \
    printf(_fmt)

#if DEBUG
#define dbgprt(_fmt...) \
    do { \
        if (opt_d) \
            _dbgprt(_fmt); \
    } while (0)
#else
#define dbgprt(_fmt...)         do { } while (0)
#endif

#if DEBUG
#define COPYX(_chr) \
    do { \
        *dst   = _chr; \
        copy = 1; \
        if (_chr != src[-1]) \
            _dbgprt(" COPY %s\n",_showchr(_chr,1)); \
    } while (0)
#else
#define COPYX(_chr) \
    do { \
        *dst   = _chr; \
    } while (0)
#endif

#define COPY \
    COPYX(chr)

// output accumulated white space and increment count of number of words
#define SPACEOUT \
    do { \
        if (! spc) \
            break; \
        spc = 0; \
        COPYX(' '); \
        ctr_word  = 1; \
    } while (0)

const char *
_showchr(int chr,int xflg)
{
    static char buf[10];
    int lo = xflg ? 0x21 : 0x20;

    if ((chr >= lo) && (chr <= 0x7E))
        sprintf(buf,"%c",chr);
    else
        sprintf(buf,"{%2.2X}",chr);

    return buf;
}

const char *
showchr(int chr)
{

    return _showchr(chr,opt_x);
}

void
showbuf(const char *buf,const char *who)
{
    const char *sep = "'";

    printf("%s: %s",who,sep);

    for (int chr = *buf  ;  chr != 0;  chr = *buf  )
        printf("%s",showchr(chr));

    printf("%s\n",sep);
}

void
dbgint(const char *sym,int val)
{
    do {
        int ctr = isupper(sym[0]);

        if (! ctr) {
            if (! val)
                break;
        }

        _dbgprt(" %s",sym);

        if (ctr)
            _dbgprt("%d",val);
    } while (0);
}

int
main(int argc,char **argv)
{
    char inp[1000];
    char buf[1000];
    char *src;
    char *dst;

    --argc;
      argv;

    for (;  argc > 0;  --argc,   argv) {
        char *cp = *argv;
        if (*cp != '-')
            break;

        cp  = 2;
        switch (cp[-1]) {
        case 'd':
            opt_d = ! opt_d;
            break;
        case 'q':
            opt_quo = ! opt_quo;
            break;
        case 'x':
            opt_x = (*cp != 0) ? atoi(cp) : 1;
            break;
        }
    }

    opt_quo = ! opt_quo;

    const char *file;
    if (argc > 0)
        file = *argv;
    else
        file = "inp.txt";

    FILE *xfsrc = fopen(file,"r");
    if (xfsrc == NULL) {
        perror(file);
        exit(1);
    }

    while (fgets(inp,sizeof(inp),xfsrc) != NULL) {
        strcpy(buf,inp);

        src = buf;
        dst = buf;

        // state variables
        int quo = 0;  // 1=within quoted string
        int spc = 0;  // 1=space seen
        int dot = 1;  // 1=period/newline seen

        // counters
        int ctr_sent = 0;  // number of sentences
        int ctr_word = 0;  // number of words
        int ctr_para = 1;  // number of paragraphs

#if DEBUG
        int copy = 0;
        int ochr = 0;
#endif

        for (int chr = *src  ;  chr != 0;  chr = *src  ) {
#if DEBUG
            if (opt_d) {
                // show if we skipped the prior char (and it was _not_ a space)
                if ((! copy) && (ochr != ' '))
                    _dbgprt("SKIP\n");
                copy = 0;
                ochr = chr;

                _dbgprt("LOOP %s",showchr(chr));
                dbgint("W:",ctr_word);
                dbgint("S:",ctr_sent);
                dbgint("P:",ctr_para);
                dbgint("quo",quo);
                dbgint("spc",spc);
                dbgint("dot",dot);
                _dbgprt("\n");
            }
#endif

            // got a quote
            if (chr == '"') {
                // flush whitespace if starting a quoted string
                if (! quo)
                    SPACEOUT;

                // copy the quote
                if (opt_quo)
                    COPY;

                // flip the quote mode
                quo = ! quo;
                continue;
            }

            // non-quote
            else {
                // if inside a quoted string, just copy out the char verbatim
                if (quo) {
                    COPY;
                    continue;
                }
            }

            // got a dot
            if (chr == '.') {
                dot = 1;

                // double dot --> newline (new paragraph)
                if (*src == '.') {
                    COPYX('\n');
                      src;
                    ctr_para  = 1;
                    continue;
                }

                COPY;

                // force whitespace mode (ensure space after dot)
                // (e.g.) change:
                //   i go.he goes.
                // into:
                //   i go. he goes.
                spc = 1;
                continue;
            }

            // from fgets, this can _only_ occur at the end of the buffer
            if (chr == '\n') {
                dot = 1;
                spc = 1;
                COPY;
                break;
            }

            // accumulate/skip over whitespace
            if (chr == ' ') {
                spc = 1;
                continue;
            }

            // output accumulated whitespace
            SPACEOUT;

            // convert case
            if (dot)
                chr = toupper(chr);
            else
                chr = tolower(chr);

            // output the current character -- it's _not_ special
            COPY;

            // count sentences
            if (dot)
                ctr_sent  = 1;

            // we're no longer at the start of a sentence
            dot = 0;
        }

        *dst = 0;

        if (opt_x == 1)
            opt_x = 0;
        showbuf(inp,"inp");
        showbuf(buf,"buf");

#if 0
        if (dot)
            ctr_word  = 1;
#endif

        printf("TOTAL: length=%zu sentences=%d paragraphs=%d words=%d\n",
            strlen(buf),ctr_sent,ctr_para,ctr_word);
    }

    fclose(xfsrc);

    return 0;
}

輸出-d

SKIP
LOOP t W:0 S:0 P:1 dot
LOOP h W:0 S:1 P:1
LOOP e W:0 S:1 P:1
LOOP   W:0 S:1 P:1
LOOP L W:0 S:1 P:1 spc
 COPY {20}
LOOP A W:1 S:1 P:1
LOOP N W:1 S:1 P:1
LOOP G W:1 S:1 P:1
LOOP U W:1 S:1 P:1
LOOP A W:1 S:1 P:1
LOOP G W:1 S:1 P:1
LOOP E W:1 S:1 P:1
LOOP   W:1 S:1 P:1
LOOP   W:1 S:1 P:1 spc
LOOP " W:1 S:1 P:1 spc
 COPY {20}
LOOP C W:2 S:1 P:1 quo
LOOP " W:2 S:1 P:1 quo
LOOP   W:2 S:1 P:1
LOOP i W:2 S:1 P:1 spc
 COPY {20}
LOOP s W:3 S:1 P:1
LOOP   W:3 S:1 P:1
LOOP a W:3 S:1 P:1 spc
 COPY {20}
LOOP   W:4 S:1 P:1
LOOP p W:4 S:1 P:1 spc
 COPY {20}
LOOP r W:5 S:1 P:1
LOOP o W:5 S:1 P:1
LOOP c W:5 S:1 P:1
LOOP e W:5 S:1 P:1
LOOP d W:5 S:1 P:1
LOOP u W:5 S:1 P:1
LOOP r W:5 S:1 P:1
LOOP a W:5 S:1 P:1
LOOP l W:5 S:1 P:1
LOOP   W:5 S:1 P:1
LOOP   W:5 S:1 P:1 spc
LOOP   W:5 S:1 P:1 spc
LOOP   W:5 S:1 P:1 spc
LOOP   W:5 S:1 P:1 spc
LOOP   W:5 S:1 P:1 spc
LOOP   W:5 S:1 P:1 spc
LOOP   W:5 S:1 P:1 spc
LOOP   W:5 S:1 P:1 spc
LOOP   W:5 S:1 P:1 spc
LOOP   W:5 S:1 P:1 spc
LOOP   W:5 S:1 P:1 spc
LOOP   W:5 S:1 P:1 spc
LOOP   W:5 S:1 P:1 spc
LOOP p W:5 S:1 P:1 spc
 COPY {20}
LOOP r W:6 S:1 P:1
LOOP o W:6 S:1 P:1
LOOP g W:6 S:1 P:1
LOOP r W:6 S:1 P:1
LOOP a W:6 S:1 P:1
LOOP m W:6 S:1 P:1
LOOP m W:6 S:1 P:1
LOOP i W:6 S:1 P:1
LOOP n W:6 S:1 P:1
LOOP g W:6 S:1 P:1
LOOP   W:6 S:1 P:1
LOOP l W:6 S:1 P:1 spc
 COPY {20}
LOOP a W:7 S:1 P:1
LOOP n W:7 S:1 P:1
LOOP g W:7 S:1 P:1
LOOP u W:7 S:1 P:1
LOOP a W:7 S:1 P:1
LOOP g W:7 S:1 P:1
LOOP e W:7 S:1 P:1
LOOP   W:7 S:1 P:1
LOOP   W:7 S:1 P:1 spc
LOOP   W:7 S:1 P:1 spc
LOOP   W:7 S:1 P:1 spc
LOOP   W:7 S:1 P:1 spc
LOOP . W:7 S:1 P:1 spc
LOOP I W:7 S:1 P:1 spc dot
 COPY {20}
LOOP t W:8 S:2 P:1
LOOP   W:8 S:2 P:1
LOOP w W:8 S:2 P:1 spc
 COPY {20}
LOOP a W:9 S:2 P:1
LOOP s W:9 S:2 P:1
LOOP   W:9 S:2 P:1
LOOP i W:9 S:2 P:1 spc
 COPY {20}
LOOP n W:10 S:2 P:1
LOOP i W:10 S:2 P:1
LOOP t W:10 S:2 P:1
LOOP i W:10 S:2 P:1
LOOP a W:10 S:2 P:1
LOOP l W:10 S:2 P:1
LOOP l W:10 S:2 P:1
LOOP y W:10 S:2 P:1
LOOP   W:10 S:2 P:1
LOOP d W:10 S:2 P:1 spc
 COPY {20}
LOOP e W:11 S:2 P:1
LOOP v W:11 S:2 P:1
LOOP e W:11 S:2 P:1
LOOP l W:11 S:2 P:1
LOOP o W:11 S:2 P:1
LOOP p W:11 S:2 P:1
LOOP e W:11 S:2 P:1
LOOP d W:11 S:2 P:1
LOOP   W:11 S:2 P:1
LOOP b W:11 S:2 P:1 spc
 COPY {20}
LOOP y W:12 S:2 P:1
LOOP   W:12 S:2 P:1
LOOP " W:12 S:2 P:1 spc
 COPY {20}
LOOP D W:13 S:2 P:1 quo
LOOP e W:13 S:2 P:1 quo
LOOP n W:13 S:2 P:1 quo
LOOP n W:13 S:2 P:1 quo
LOOP i W:13 S:2 P:1 quo
LOOP s W:13 S:2 P:1 quo
LOOP   W:13 S:2 P:1 quo
LOOP R W:13 S:2 P:1 quo
LOOP i W:13 S:2 P:1 quo
LOOP t W:13 S:2 P:1 quo
LOOP c W:13 S:2 P:1 quo
LOOP h W:13 S:2 P:1 quo
LOOP i W:13 S:2 P:1 quo
LOOP e W:13 S:2 P:1 quo
LOOP " W:13 S:2 P:1 quo
LOOP . W:13 S:2 P:1
 COPY {0A}
LOOP   W:13 S:2 P:2 dot
LOOP   W:13 S:2 P:2 spc dot
LOOP   W:13 S:2 P:2 spc dot
LOOP   W:13 S:2 P:2 spc dot
LOOP   W:13 S:2 P:2 spc dot
LOOP   W:13 S:2 P:2 spc dot
LOOP   W:13 S:2 P:2 spc dot
LOOP   W:13 S:2 P:2 spc dot
LOOP   W:13 S:2 P:2 spc dot
LOOP   W:13 S:2 P:2 spc dot
LOOP   W:13 S:2 P:2 spc dot
LOOP   W:13 S:2 P:2 spc dot
LOOP t W:13 S:2 P:2 spc dot
 COPY {20}
 COPY T
LOOP h W:14 S:3 P:2
LOOP e W:14 S:3 P:2
LOOP   W:14 S:3 P:2
LOOP M W:14 S:3 P:2 spc
 COPY {20}
 COPY m
LOOP a W:15 S:3 P:2
LOOP i W:15 S:3 P:2
LOOP n W:15 S:3 P:2
LOOP   W:15 S:3 P:2
LOOP f W:15 S:3 P:2 spc
 COPY {20}
LOOP e W:16 S:3 P:2
LOOP A W:16 S:3 P:2
 COPY a
LOOP t W:16 S:3 P:2
LOOP u W:16 S:3 P:2
LOOP r W:16 S:3 P:2
LOOP e W:16 S:3 P:2
LOOP s W:16 S:3 P:2
LOOP   W:16 S:3 P:2
LOOP o W:16 S:3 P:2 spc
 COPY {20}
LOOP f W:17 S:3 P:2
LOOP   W:17 S:3 P:2
LOOP " W:17 S:3 P:2 spc
 COPY {20}
LOOP C W:18 S:3 P:2 quo
LOOP " W:18 S:3 P:2 quo
LOOP   W:18 S:3 P:2
LOOP l W:18 S:3 P:2 spc
 COPY {20}
LOOP a W:19 S:3 P:2
LOOP n W:19 S:3 P:2
LOOP g W:19 S:3 P:2
LOOP u W:19 S:3 P:2
LOOP a W:19 S:3 P:2
LOOP g W:19 S:3 P:2
LOOP e W:19 S:3 P:2
LOOP   W:19 S:3 P:2
LOOP i W:19 S:3 P:2 spc
 COPY {20}
LOOP n W:20 S:3 P:2
LOOP c W:20 S:3 P:2
LOOP l W:20 S:3 P:2
LOOP u W:20 S:3 P:2
LOOP d W:20 S:3 P:2
LOOP e W:20 S:3 P:2
LOOP   W:20 S:3 P:2
LOOP l W:20 S:3 P:2 spc
 COPY {20}
LOOP o W:21 S:3 P:2
LOOP w W:21 S:3 P:2
LOOP - W:21 S:3 P:2
LOOP l W:21 S:3 P:2
LOOP e W:21 S:3 P:2
LOOP v W:21 S:3 P:2
LOOP e W:21 S:3 P:2
LOOP l W:21 S:3 P:2
LOOP   W:21 S:3 P:2
LOOP a W:21 S:3 P:2 spc
 COPY {20}
LOOP c W:22 S:3 P:2
LOOP c W:22 S:3 P:2
LOOP e W:22 S:3 P:2
LOOP s W:22 S:3 P:2
LOOP s W:22 S:3 P:2
LOOP   W:22 S:3 P:2
LOOP t W:22 S:3 P:2 spc
 COPY {20}
LOOP o W:23 S:3 P:2
LOOP   W:23 S:3 P:2
LOOP m W:23 S:3 P:2 spc
 COPY {20}
LOOP e W:24 S:3 P:2
LOOP m W:24 S:3 P:2
LOOP o W:24 S:3 P:2
LOOP r W:24 S:3 P:2
LOOP y W:24 S:3 P:2
LOOP , W:24 S:3 P:2
LOOP   W:24 S:3 P:2
LOOP s W:24 S:3 P:2 spc
 COPY {20}
LOOP i W:25 S:3 P:2
LOOP m W:25 S:3 P:2
LOOP p W:25 S:3 P:2
LOOP l W:25 S:3 P:2
LOOP e W:25 S:3 P:2
LOOP   W:25 S:3 P:2
LOOP s W:25 S:3 P:2 spc
 COPY {20}
LOOP e W:26 S:3 P:2
LOOP t W:26 S:3 P:2
LOOP   W:26 S:3 P:2
LOOP o W:26 S:3 P:2 spc
 COPY {20}
LOOP f W:27 S:3 P:2
LOOP   W:27 S:3 P:2
LOOP k W:27 S:3 P:2 spc
 COPY {20}
LOOP e W:28 S:3 P:2
LOOP y W:28 S:3 P:2
LOOP w W:28 S:3 P:2
LOOP o W:28 S:3 P:2
LOOP r W:28 S:3 P:2
LOOP d W:28 S:3 P:2
LOOP s W:28 S:3 P:2
LOOP , W:28 S:3 P:2
LOOP   W:28 S:3 P:2
LOOP a W:28 S:3 P:2 spc
 COPY {20}
LOOP n W:29 S:3 P:2
LOOP d W:29 S:3 P:2
LOOP   W:29 S:3 P:2
LOOP c W:29 S:3 P:2 spc
 COPY {20}
LOOP l W:30 S:3 P:2
LOOP e W:30 S:3 P:2
LOOP a W:30 S:3 P:2
LOOP n W:30 S:3 P:2
LOOP   W:30 S:3 P:2
LOOP s W:30 S:3 P:2 spc
 COPY {20}
LOOP t W:31 S:3 P:2
LOOP y W:31 S:3 P:2
LOOP l W:31 S:3 P:2
LOOP e W:31 S:3 P:2
LOOP   W:31 S:3 P:2
LOOP   W:31 S:3 P:2 spc
LOOP   W:31 S:3 P:2 spc
LOOP   W:31 S:3 P:2 spc
LOOP   W:31 S:3 P:2 spc
LOOP   W:31 S:3 P:2 spc
LOOP   W:31 S:3 P:2 spc
LOOP   W:31 S:3 P:2 spc
LOOP   W:31 S:3 P:2 spc
LOOP   W:31 S:3 P:2 spc
LOOP   W:31 S:3 P:2 spc
LOOP   W:31 S:3 P:2 spc
LOOP   W:31 S:3 P:2 spc
LOOP   W:31 S:3 P:2 spc
LOOP   W:31 S:3 P:2 spc
LOOP   W:31 S:3 P:2 spc
LOOP . W:31 S:3 P:2 spc
LOOP {0A} W:31 S:3 P:2 spc dot
inp: 'the LANGUAGE  "C" is a procedural              programming language     .It was initially developed by "Dennis Ritchie"..            the Main feAtures of "C" language include low-level access to memory, simple set of keywords, and clean style                .{0A}'
buf: 'The language "C" is a procedural programming language. It was initially developed by "Dennis Ritchie"{0A} The main features of "C" language include low-level access to memory, simple set of keywords, and clean style.{0A}'
TOTAL: length=214 sentences=3 paragraphs=2 words=31

uj5u.com熱心網友回復:

這是一個可能的解決方案。

希望這將表明您不需要所有的重復。

我只用給出的例子對其進行了測驗,很可能仍然存在它可能會中斷的邊緣情況。您可能希望分配buffer而不是使用特定值,但您可能需要檢查是否有可能導致擴展的輸入。

關于正確位置的大寫字母,原文中除了一行之外沒有任何范圍,因此沒有段落的概念。因此,我在句子的開頭使用了大寫字母。

注意: OP 沒有指定正確的輸出是什么,因為該帖子的標題是“錯誤的字符數”,所以這是基于要求和 OP 代碼的一些見解的最佳猜測(可能如上所述沒有產生正確的結果)。

我不認為這里的重點是修復 OP 的錯誤,而是說明更接近或實作解決方案的替代方法。

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

char original[] = "the LANGUAGE  \"C\" is a procedural              programming language     .It was initially developed by \"Dennis Ritchie\"..            the Main feAtures of \"C\" language include low-level access to memory, simple set of keywords, and clean style                .";

int main() {
    
    char buffer[256];
    strcpy(buffer, original);
    char *src, *dest;
    
    bool quoted = false;
    bool sentence = false;
    int periods = 0;
    bool space = false;
    bool paragraph = true;
    bool comma = false;
    int letters = 0;
    int words = 0;
    int sentences = 0;
    int paragraphs = 0;
    
    src = dest = buffer;

    for ( ; *src ; src  ) {
        
        if (quoted) {

            switch (*src) {
                
            case '"': 
                quoted = false;
                break;
                
            }
        
            if ((*src >= 'a' && *src <= 'z') || (*src >= 'a' && *src <= 'z')) {
                letters  ;
            } else {
                if (letters) {
                    words  ;
                    letters = 0;
                }
            }
            
            *dest   = *src;

        } else {
            
            switch (*src) {
                
            case '"':
                quoted = true;
                break;
                
            case ',':
                comma = true;
                continue;

            case ' ':
                if (letters) {
                    words  ;
                    letters = 0;
                }
                space = true;
                continue;

            case '.':
                if (  periods == 2) {
                    *dest   = '.';
                    *dest   = '\n';
                    periods = 0;
                    paragraph = true;
                } else {
                    sentence = true;
                }
                continue;
            }
            
            if (comma) {
                *dest   = ',';
                *dest   = ' ';
                comma = space = false;
            }
            
            if (periods) {
                *dest   = '.';
                periods = 0;
            }

            if (space) {
                if (!paragraph) {
                    *dest   = ' ';
                }
                space = false;
            }
            
            if ((*src >= 'a' && *src <= 'z') || (*src >= 'a' && *src <= 'z')) {
                letters  ;
            } else {
                if (letters) {
                    words  ;
                    letters = 0;
                }
            }
            
            *dest   = sentence || paragraph ? toupper(*src) : tolower(*src);

            if (sentence || paragraph) {
                if (letters) {
                    words  ;
                }
                letters = 0;
            }
            
            if (sentence) {
                sentences  ;
            }
            
            if (paragraph) {
                paragraphs  ;
            }
            
            sentence = paragraph = false;
        }

    }
    
    if (sentence) {
        sentences  ;
    }
    
    if (paragraph) {
        paragraphs  ;
    }
    
    if (periods) {
        *dest   = '.';
    }

    *dest   = '\n';

    *dest = '\0';

    printf("\nInput Chars=%d\n\n\"%s\"\n", (int)strlen(original), original);
    printf("\nOutput Chars=%d, Words=%d, Sentences=%d, Paragraphs=%d\n\n\"%s\"\n", (int)strlen(buffer), words, sentences, paragraphs, buffer);
        
    return 0;
}

這會產生:

Input Chars=259

"the LANGUAGE  "C" is a procedural              programming language     .It was initially developed by "Dennis Ritchie"..            the Main feAtures of "C" language include low-level access to memory, simple set of keywords, and clean style                ."

Output Chars=214, Words=34, Sentences=3, Paragraphs=2

"The language "C" is a procedural programming language. It was initially developed by "Dennis Ritchie".
The main features of "C" language include low-level access to memory, simple set of keywords, and clean style.
"

轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/473795.html

標籤:C 细绳 循环 空白

上一篇:我對最后一個for回圈有問題。它顯示我只是陣列中的最后一個學生,但應該都是5

下一篇:如何根據兩個條件將資料框的單元格值相乘?

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • 【從零開始擼一個App】Dagger2

    Dagger2是一個IOC框架,一般用于Android平臺,第一次接觸的朋友,一定會被搞得暈頭轉向。它延續了Java平臺Spring框架代碼碎片化,注解滿天飛的傳統。嘗試將各處代碼片段串聯起來,理清思緒,真不是件容易的事。更不用說還有各版本細微的差別。 與Spring不同的是,Spring是通過反射 ......

    uj5u.com 2020-09-10 06:57:59 more
  • Flutter Weekly Issue 66

    新聞 Flutter 季度調研結果分享 教程 Flutter+FaaS一體化任務編排的思考與設計 詳解Dart中如何通過注解生成代碼 GitHub 用對了嗎?Flutter 團隊分享如何管理大型開源專案 插件 flutter-bubble-tab-indicator A Flutter librar ......

    uj5u.com 2020-09-10 06:58:52 more
  • Proguard 常用規則

    介紹 Proguard 入口,如何查看輸出,如何使用 keep 設定入口以及使用實體,如何配置壓縮,混淆,校驗等規則。

    ......

    uj5u.com 2020-09-10 06:59:00 more
  • Android 開發技術周報 Issue#292

    新聞 Android即將獲得類AirDrop功能:可向附近設備快速分享檔案 谷歌為安卓檔案管理應用引入可安全隱藏資料的Safe Folder功能 Android TV新主界面將顯示電影、電視節目和應用推薦內容 泄露的Android檔案暗示了傳說中的谷歌Pixel 5a與折疊屏新機 谷歌發布Andro ......

    uj5u.com 2020-09-10 07:00:37 more
  • AutoFitTextureView Error inflating class

    報錯: Binary XML file line #0: Binary XML file line #0: Error inflating class xxx.AutoFitTextureView 解決: <com.example.testy2.AutoFitTextureView android: ......

    uj5u.com 2020-09-10 07:00:41 more
  • 根據Uri,Cursor沒有獲取到對應的屬性

    Android: 背景:呼叫攝像頭,拍攝視頻,指定保存的地址,但是回傳的Cursor檔案,只有名稱和大小的屬性,沒有其他諸如時長,連ID屬性都沒有 使用 cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DURATIO ......

    uj5u.com 2020-09-10 07:00:44 more
  • Android連載29-持久化技術

    一、持久化技術 我們平時所使用的APP產生的資料,在記憶體中都是瞬時的,會隨著斷電、關機等丟失資料,因此android系統采用了持久化技術,用于存盤這些“瞬時”資料 持久化技術包括:檔案存盤、SharedPreference存盤以及資料庫存盤,還有更復雜的SD卡記憶體儲。 二、檔案存盤 最基本存盤方式, ......

    uj5u.com 2020-09-10 07:00:47 more
  • Android Camera2Video整合到自己專案里

    背景: Android專案里呼叫攝像頭拍攝視頻,原本使用的 MediaStore.ACTION_VIDEO_CAPTURE, 后來因專案需要,改成了camera2 1.Camera2Video 官方demo有點問題,下載后,不能直接整合到專案 問題1.多次拍攝視頻崩潰 問題2.雙擊record按鈕, ......

    uj5u.com 2020-09-10 07:00:50 more
  • Android 開發技術周報 Issue#293

    新聞 谷歌為Android TV開發者提供多種新功能 Android 11將自動填表功能整合到鍵盤輸入建議中 谷歌宣布Android Auto即將支持更多的導航和數字停車應用 谷歌Pixel 5只有XL版本 搭載驍龍765G且將比Pixel 4更便宜 [圖]Wear OS將迎來重磅更新:應用啟動時間 ......

    uj5u.com 2020-09-10 07:01:38 more
  • 海豚星空掃碼投屏 Android 接收端 SDK 集成 六步驟

    掃碼投屏,開放網路,獨占設備,不需要額外下載軟體,微信掃碼,發現設備。支持標準DLNA協議,支持倍速播放。視頻,音頻,圖片投屏。好點意思。還支持自定義基于 DLNA 擴展的操作動作。好像要收費,沒體驗。 這里簡單記錄一下集成程序。 一 跟目錄的build.gradle添加私有mevan倉庫 mave ......

    uj5u.com 2020-09-10 07:01:43 more
最新发布
  • 歡迎頁輪播影片

    如圖,引導開始,球從上落下,同時淡入文字,然后文字開始輪播,最后一頁時停止,點擊進入首頁。 在來看看效果圖。 重力球先不講,主要歡迎輪播簡單實作 首先新建一個類 TextTranslationXGuideView,用于影片展示 文本是類似的,最后會有個圖片箭頭影片,布局很簡單,就是一個 TextVi ......

    uj5u.com 2023-04-20 08:40:31 more
  • 【FAQ】關于華為推送服務因營銷訊息頻次管控導致服務通訊類訊息

    一. 問題描述 使用華為推送服務下發IM訊息時,下發訊息請求成功且code碼為80000000,但是手機總是收不到訊息; 在華為推送自助分析(Beta)平臺查看發現,訊息發送觸發了頻控。 二. 問題原因及背景 2023年1月05日起,華為推送服務對咨詢營銷類訊息做了單個設備每日推送數量上限管理,具體 ......

    uj5u.com 2023-04-20 08:40:11 more
  • 歡迎頁輪播影片

    如圖,引導開始,球從上落下,同時淡入文字,然后文字開始輪播,最后一頁時停止,點擊進入首頁。 在來看看效果圖。 重力球先不講,主要歡迎輪播簡單實作 首先新建一個類 TextTranslationXGuideView,用于影片展示 文本是類似的,最后會有個圖片箭頭影片,布局很簡單,就是一個 TextVi ......

    uj5u.com 2023-04-20 08:39:36 more
  • 【FAQ】關于華為推送服務因營銷訊息頻次管控導致服務通訊類訊息

    一. 問題描述 使用華為推送服務下發IM訊息時,下發訊息請求成功且code碼為80000000,但是手機總是收不到訊息; 在華為推送自助分析(Beta)平臺查看發現,訊息發送觸發了頻控。 二. 問題原因及背景 2023年1月05日起,華為推送服務對咨詢營銷類訊息做了單個設備每日推送數量上限管理,具體 ......

    uj5u.com 2023-04-20 08:39:13 more
  • iOS從UI記憶體地址到讀取成員變數(oc/swift)

    開發除錯時,我們發現bug時常首先是從UI顯示發現例外,下一步才會去定位UI相關連的資料的。XCode有給我們提供一系列debug工具,但是很多人可能還沒有形成一套穩定的除錯流程,因此本文嘗試解決這個問題,順便提出一個暴論:UI顯示例外問題只需要兩個步驟就能完成定位作業的80%: 定位例外 UI 組 ......

    uj5u.com 2023-04-19 09:16:23 more
  • FIDE重磅更新!性能飛躍!體驗有禮!

    FIDE 開發者工具重構升級啦!實作500%性能提升,誠邀體驗! 一直以來不少開發者朋友在社區反饋,在使用 FIDE 工具的程序中,時常會遇到諸如加載不及時、代碼預覽/渲染性能不如意的情況,十分影響開發體驗。 作為技術團隊,我們深知一件趁手的開發工具對開發者的重要性,因此,在2023年開年,FinC ......

    uj5u.com 2023-04-19 09:16:15 more
  • 游戲內嵌社區服務開放,助力開發者提升玩家互動與留存

    華為 HMS Core 游戲內嵌社區服務提供快速訪問華為游戲中心論壇能力,支持玩家直接在游戲內瀏覽帖子和交流互動,助力開發者擴展內容生產和觸達的場景。 一、為什么要游戲內嵌社區? 二、游戲內嵌社區的典型使用場景 1、游戲內打開論壇 您可以在游戲內繪制論壇入口,為玩家提供沉浸式發帖、瀏覽、點贊、回帖、 ......

    uj5u.com 2023-04-19 09:15:46 more
  • iOS從UI記憶體地址到讀取成員變數(oc/swift)

    開發除錯時,我們發現bug時常首先是從UI顯示發現例外,下一步才會去定位UI相關連的資料的。XCode有給我們提供一系列debug工具,但是很多人可能還沒有形成一套穩定的除錯流程,因此本文嘗試解決這個問題,順便提出一個暴論:UI顯示例外問題只需要兩個步驟就能完成定位作業的80%: 定位例外 UI 組 ......

    uj5u.com 2023-04-19 09:14:53 more
  • FIDE重磅更新!性能飛躍!體驗有禮!

    FIDE 開發者工具重構升級啦!實作500%性能提升,誠邀體驗! 一直以來不少開發者朋友在社區反饋,在使用 FIDE 工具的程序中,時常會遇到諸如加載不及時、代碼預覽/渲染性能不如意的情況,十分影響開發體驗。 作為技術團隊,我們深知一件趁手的開發工具對開發者的重要性,因此,在2023年開年,FinC ......

    uj5u.com 2023-04-19 09:14:08 more
  • 游戲內嵌社區服務開放,助力開發者提升玩家互動與留存

    華為 HMS Core 游戲內嵌社區服務提供快速訪問華為游戲中心論壇能力,支持玩家直接在游戲內瀏覽帖子和交流互動,助力開發者擴展內容生產和觸達的場景。 一、為什么要游戲內嵌社區? 二、游戲內嵌社區的典型使用場景 1、游戲內打開論壇 您可以在游戲內繪制論壇入口,為玩家提供沉浸式發帖、瀏覽、點贊、回帖、 ......

    uj5u.com 2023-04-19 09:08:34 more