繁体   English   中英

用 c 计算字符串中的单词

[英]Counting words in string with c

the push to vaccinate children has taken on fresh urgency amid concerns that the new omicron variant of the virus first identified in southern africa and hong kong in late november will spread quickly in the united states causing a surge in infections already back on the rise from the easily transmitted delta variant given the pervasiveness of delta and prospects of new variants spreading in the united states having as much immunity in the population as possible is critical said dr amesh adalja senior scholar at the johns hopkins center for health security

这是我的任务:

  • 将单词之间的多个空格替换为一个空格,并删除开头和结尾不必要的空格。
  • 数单词
  • 打印编辑的字符串
  • 不要使用新字符串,只需编辑。

我找不到问题。 它应该数单词,但它不能。 请帮帮我。

//Counting words program C
#include <stdio.h>

#define N 5000

int main(void) {
    FILE *fp;
    char text[N];
    
    int k, d, leng, spacecount = 0;
    int m, j, z, i, p, n;

    if ((fp = fopen("soru.txt", "r")) == NULL) {
        printf("Dosya acma hatasi!");
        return 1;
    }
    
    fgets(text, N - 1, fp);
    
    while (k < N && text[k] != '\0') {
        leng++;
        k++;
    }
    
    z = leng;
    
    for (i = 0; i < leng; i++) {
        if (i = 0 && text[i] == ' ') {
            z--;
            for (m = 0; m < leng; m++) {
                text[m] = text[m + 1];
            }
            i--;
            text[z] == '\0';
        } else
        if (text[i] ==' ' && text[i + 1] == ' ') {
            z--;
            for (j = i; j < leng; j++) {
                text[j + 1] = text[j + 2];
            }
            i--;
            text[z] == '\0';
        } else
        if (text[i] == ' ' && text[i + 1] == '\0') {
            z--;
            for (j = i; j < leng; j++) {
                text[j] = text[j + 1];
            }
            i--;
            text[z] == '\0';
        } else
        if (text[i] == '\0') {
            break;
        }
    }

    while (text[d] != '\0') {
        if (text[d] == ' ') 
            spacecount++;
        d++;
    }
        
    printf("kelime sayisi: %d" , spacecount + 1);
    printf("\n cikti:%s ", text);
    
    fclose(fp);
    
    return 0;
}

我找不到问题。 它应该计算这个词,但它不能这样做。 请帮帮我

for(i=0; i < leng; i++) {

    if(i=0 && text[i]== ' '){

        z--;
        for(m=0; m< leng; m++ ){

        text[m] = text [m+1];}
        i--;

        }

    else if(1<i<z && text[i] ==' ' && text[i+1] == ' ' ){
        z--;
        for(j=i; j<leng ; j++) {

        text[j+1] = text [j+2];}
        i--;

        } 

    else if(i=z && text[i] ==' ' && text[i+1] == '\0' ){
        z--;
        for(j=i; j<leng ; j++) {

        text[j] = text [j+1];    }
        i--;

        }

},// I think problem in here. Endless loop


你的代码太复杂了。 您可以使用 2 个索引变量来解决问题:一个从输入行读取字符,一个将相关字符写入同一个缓冲区。

您将跟踪前一个字符,从空格开始,并检测单词的开头,因为当前字符不是空格后面的空格。 因此,您将计算单词,并且除了一行中的第一个单词之外,每个单词之前只有一个空格 output 。

这是修改后的版本:

//Counting words program C
#include <stdio.h>

#define N 5000

int main(void) {
    FILE *fp;
    char text[N];
    int total_words = 0;

    if ((fp = fopen("soru.txt", "r")) == NULL) {
        printf("Dosya açma hatası!\n");
        return 1;
    }

    while (fgets(text, N, fp) != NULL) {
        int len = strlen(text);
        int word_count = 0;
        char c, lastc = ' ';
        int i, j;

        // strip the trailing newline
        if (len > 0 && text[len - 1] == '\n') {
            text[--len] == '\0';
        }
        for (i = j = 0; i < len; i++) {
            c = text[i];
            if (c != ' ') {
                if (lastc == ' ') {
                    if (word_count > 0) {
                        // output a space between words
                        text[j++] = ' ';
                    }
                    word_count++;
                }
                text[j++] = c; // copy the non space character
            }
            lastc = c;
        }
        text[j] = '\0';  // set the null terminator
        printf("kelime sayısı: %d\n", word_count);
        printf("çıktı: %s\n", text);
        total_words += word_count;
    }           
    fclose(fp);
    printf("\ntoplam kelime sayısı: %d\n", total_words);
    
    return 0;
}

请注意代码中的一个愚蠢错误: if (i = 0 && text[i] == ' ')被解析为if ((i = (0 && (text[i] == ' '))) != 0)这始终为 false 并将i设置为值0 C 表达式语法非常强大,但有些容易出错和混乱。 我建议您使用-Wall-Weverything作为编译器选项,让编译器警告潜在的错误。

同样,你不应该写if (1<i<z &&... : 1<i<z被解析为1<(i<z)这总是假的。你必须写1 < i && i < z或更多惯用的i > 1 && i < z

暂无
暂无

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

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