Home > OS >  Counting words in string with c
Counting words in string with c

Time:12-12

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

This is my assignment:

  • replace multiple spaces to one space between words and delete unnecessary spaces at the beginning and the end.
  • count the words
  • print edited string
  • dont use a new string, just edit.

I can't find problem. It should count the words but it can not do. Help me, please.

//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;
}

I can't find problem. It should count the word but it can not do. Help me, please

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


CodePudding user response:

Your code is too complicated. You can solve the problem with 2 index variables: one to read the characters from the input line, one to write the relevant characters into the same buffer.

You would keep track of the previous character, starting with space, and detect the beginning of words as the current character is not a space following a space. You would thus count the words and only output a space before each word except the first on a line.

Here is a modified version:

//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;
}

Note a silly bug in your code: if (i = 0 && text[i] == ' ') is parsed as if ((i = (0 && (text[i] == ' '))) != 0) which is always false and sets i to the value 0. C expression syntax is very powerful but somewhat error prone and confusing. I advise you to use -Wall or -Weverything as a compiler option to let the compiler warn about potential mistakes.

Similarly, you should not write if (1<i<z && ...: 1<i<z is parsed as 1<(i<z) which is always false. You must write 1 < i && i < z or more idiomatically i > 1 && i < z

  • Related