0
int main()
{
    char a[1000] = {0};
    float L;
    
    //read a line from input
    printf("Text:");
    scanf("%[^\n]s", a);
    
    float word_count = 0;
    float letter_count = 0;
    int sent_count = 0;
    int idx = 0; 
    
    // go through the line
    while (a[idx]){
         
         // if next char is special char, then we found a sentence
         while (a[idx] == '.' || a[idx] == '?' || a[idx] == '!' || a[idx] == '\n')
            sent_count++;

I was expecting the above while loop to evaluate to TRUE when special characters are detected which then would increment sent_count but it doesnt work. Sent_count is always zero. I can't figure out where I am going wrong?

        //skip spaces
        while(a[idx] && isspace(a[idx]))
            idx++;
            
        // if next char is a letter => we found a word
        if (a[idx])
            word_count++;
        
        //skip the word, increment number of letters
        while (a[idx] && !isspace(a[idx])){
            letter_count++;
            idx++;
        }
        
    }
    
    printf("word count = %f\nletter count = %f\n", word_count, letter_count);
   
    L = letter_count/word_count*100;
    
    printf("L= %.2f\n", L);
    printf("# of Sentences: %d\n", sent_count);
}
Paul R
  • 208,748
  • 37
  • 389
  • 560
Pike1406
  • 5
  • 2
  • 3
    Why on earth are letter_count and word_count floats? – klutt Jul 10 '20 at 18:28
  • 2
    When `sent_float` is incremented, you are inside an infinite loop. – MikeCAT Jul 10 '20 at 18:29
  • A) Use functions, break up your code into smaller parts that are easier to test and understand. B) Declare variables closer to where they're used. – tadman Jul 10 '20 at 18:38
  • 1
    @MikeCAT where do you see `sent_float`? – Pike1406 Jul 10 '20 at 18:50
  • 1
    Ouch, I mean `sent_count` – MikeCAT Jul 10 '20 at 18:52
  • @klutt Used float because wanted result to have decimal places. – Pike1406 Jul 10 '20 at 18:53
  • 1
    "Used float because wanted result to have decimal places" Like what? 3.14159265 words? – n. m. could be an AI Jul 10 '20 at 18:58
  • @MikeCAT I tried changing to `if (a[idx] == '.' || a[idx] == '?' || a[idx] == '!' || a[idx] == '\n')` but that didnt work either. I guess I don't have a firm understanding of how loops work. I thought that if the boolean expression evaluates to TRUE, then it would increment; otherwise, exit the loop which of course doesnt happen and I don't understand why not....thoughts? – Pike1406 Jul 10 '20 at 18:59
  • @Pike1406 Then you instead do something like `L = (float)letter_count/word_count*100;` – klutt Jul 10 '20 at 20:00

2 Answers2

1

sent_count remains 0 only because special characters were counted in your last while loop :

while (a[idx] && !isspace(a[idx]))

Try it with below program.

#include <stdio.h>
int main()
{
    char a[1000] = {0};
    float L;
    int flag;

    //read a line from input
    printf("Text:");
    scanf("%[^\n]s", a);

    float word_count = 0;
    float letter_count = 0;
    int sent_count = 0;
    int idx = 0;

    // go through the line
    while (a[idx]){

         // if next char is special char, then we found a sentence
         if(a[idx] == '.' || a[idx] == '?' || a[idx] == '!' || a[idx] == '\n')
         {
            sent_count++;
            idx++;
         }
//skip spaces
        while(a[idx] && isspace(a[idx]))
            idx++;

        // if next char is a letter => we found a word
/*        if (a[idx])
            word_count++;*/
        flag =0;
        //skip the word, increment number of letters
        while(a[idx] && !isspace(a[idx]) && a[idx]!='.' && a[idx]!='?' && a[idx]!='!' && a[idx]!='\n'){
            letter_count++;
            idx++;
            flag =1;
        }
        if(flag ==1 )
           word_count++;

    }

    printf("word count = %f\nletter count = %f\n", word_count, letter_count);

    L = letter_count/word_count*100;

    printf("L= %.2f\n", L);
    printf("# of Sentences: %d\n", sent_count);
}
  • This worked! You are awesome-thank you! Now I just need to study why this worked and my old code didn't. Cheers. – Pike1406 Jul 10 '20 at 19:10
  • 1
    @Pike1406: Have you tried running your code line by line in a [debugger](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems)? That way, it should be easy for you to see what is happening in your program. – Andreas Wenzel Jul 10 '20 at 19:17
  • @AndreasWenzel @anjaliagrawal So I ran debugger line-by-line and there are some steps that are confusing to me. Text input is `Hi! My name is Clint. What is your name?` When executing the very first IF statement, the program skips `sent_count++;` executing `idx++;` I thought if the IF statements evaluates to TRUE, it will execute all lines within `{}` but it doesn't do that. I am confused. Why don't all lines execute? Thanks in advance for your help. – Pike1406 Jul 10 '20 at 20:58
  • @Pike1406: You are right that the debugger should normally stop on both lines and not skip any of the lines you mentioned. Maybe you compiled the program in such a way that the debugger is having trouble determining which machine code corresponds to which line. You should create a so-called "debug build" with all compiler optimizations off, otherwise the debugger will likely have trouble. What compiler and [IDE](https://en.wikipedia.org/wiki/Integrated_development_environment) are you using? – Andreas Wenzel Jul 11 '20 at 00:00
  • @Pike1406: Here first we are checking if sentence terminator is present. If it is present, i.e first if here evaluates to true then we are incrementing sent_count. Next, we are skipping the spaces with while loop. And in the last while loop, we are counting letters if character is present and is not sentence terminator or space and if any of the letter found, then after this while we are incrementing word_count. – Anjali Agrawal Jul 11 '20 at 10:29
  • @Pike1406: As per your input, first character will come as H so it will skip first if and while and directly went to last while loop in which it will count 2 letters : H,i then increment one word after that while loop. Now it went to the start of parent while loop and next character is now '!' so now first if will evaluate to true and it will increase sent_count by 1 and proceeds so on. – Anjali Agrawal Jul 11 '20 at 10:32
  • @AnjaliAgrawal For some reason now my compiler is throwing an error message for error: expected expression ` on this line ` while(a[idx]){`. This error message wasn't present when I previously ran the compiler. Any ideas? – Pike1406 Jul 14 '20 at 16:28
  • @Pike1406, check once if any ; or braces are mistakenly deleted, or paste your code snippet so that can see what exactly is the failure – Anjali Agrawal Jul 15 '20 at 16:54
0

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

int main()
{
    char a[1000] = {0};
    
    //read from input until 0 is found
    scanf("%[^0]s", a);
    
    int word_count = 0;
    int letter_count = 0;
    int sent_count = 0;
    int idx = 0; 
    
    //count here the . ? ! and \n
    //try to at least not double count the case . ? ! followed by \n
    while(a[idx]){
        sent_count += ((a[idx] == '?' || a[idx] == '!' || a[idx] == '.' || a[idx] == '\n')); 
        idx += 1 + ((a[idx] == '?' || a[idx] == '!' || a[idx] == '.' || a[idx] == '\n'));
    }
    
    idx = 0;
    // go through the line
    while (a[idx]){
        
        //skip spaces
        while(a[idx] && isspace(a[idx]))
            idx++;
            
        // if next char is a letter => we found a word
        if (a[idx])
            word_count++;
        
        //skip the word, increment number of letters
        while (a[idx] && !isspace(a[idx])){
            letter_count++;
            idx++;
        }
    }
    
    printf("word count = %d letter count = %d sentece count = %d", word_count, letter_count, sent_count);
   
    return 0;
}

enter image description here