Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
#include <stdio.h>
#include <string.h>
#include <math.h>

int main(void)
{
char text[100];
printf("Enter text:\n");
scanf("%s",&text[100]);

//Loop to count letters words and sentences

int letters = 0;
int words = 0;
int sentences = 0;
int n = strlen (text);
for (int i=0 ; i<n ;="" i++)
{
="" if="" 
="" ((text[i]="">= 'A' && text[i] <= 'Z') || (text[i] >= 'a' && text[i] <= 'z'))
  {
  letters++;
  }
  else if 
  ((i==0 && text[i]!= ' ') ||(i != n-1 && text[i]==' ' && text[i+1]!=' '))
  {
  words++;
  }
  else if 
    (text[i] == '.' || text[i] == '!' || text[i] == '?')
  {
  sentences++;
  }
}

printf("\nLetters: %i\nWords: %i\nSentences: %i\n", letters, words, sentences);

float L = (letters / (float) words) * 100;
printf("Average No. of letters: %f\n", L);
float S = (sentences / (float) words) * 100;
printf("Average No. of sentences: %f\n", S);
float grade = (0.0588 * L) - (0.296 * S) - 15.8;
printf ("G:%f\n",grade);
  if (grade <= 16 )
  {
  printf("Grade %i\n", (int)grade);
  }
  else if (grade > 16)
  {
  printf("Grade 16+\n");
  }
  else
  {
  printf("Before Grade 1\n");
  }

}


What I have tried:

Can some one say what is the problem with this code?
I tried using for loop and while loop but my loop doesn't get executed. What is the reason ?
My for loop is not being executed.
Posted
Updated 14-Jul-20 22:32pm
v2
Comments
Rick York 13-Jul-20 19:48pm    
I recommend that you look at the isalpha and isdigit functions. They can take care of much of your conditional logic. There are a few other related functions that can help too.

You realize there are other forms of punctuation also like commas and colons, right?

Lastly, I highly recommend that you learn how to read data from files. This will save you a lot of time typing input so you can spend it writing code and debugging it. I can not emphasize this enough. You will be glad you did.

Well ... yes. And it's pretty obvious if you think about it.

You declare an array to hold the string the user enters (I'd make that bigger if I was you - it's pretty normal for even a single sentence to exceed 100 characters, this one does):
C++
char text[100];
But then you read the string in past the end of it:
C++
scanf("%s",&text[100]);
test[100] is an element outside the array: array indexes run from 0 to N - 1 where N is the number of elements in the array.
So you tell the system to read the data into "random memory" instead of into the area you set aside for it.
When you enter your loop, you try to use the original array space, and don't find any usable data in it.

Change it to this:
C++
scanf("%s",text);
And it should help, because the name of an array is a pointer to the first element.

You would have spotted this almost immediately if you had used the debugger to look at what your code was doing while it was running ...
 
Share this answer
 
Try
C++
scanf("%s",&text);

Quote:
I tried using for loop and while loop but my loop doesn't get executed. What is the reason ?

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
I solved it. Thanks all !!

C++
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <ctype.h>


int main(void)

{

    char text[1000];
    char* fgets(char *text, int length, FILE * stream);
    puts("Enter text:");
    fgets(text,sizeof(text),stdin);

    int letters =  0, words = 0, sentences = 0, n = strlen(text);
    
//Loop to count letters words and sentences

    for (int i = 0 ; i < n ; i ++)
    {
        if (isalpha(text[i]))
        {
            letters++;
        }
        else if (text[i] == 32 && (text[i - 1] != 33 && text[i - 1] != 46 && text[i - 1] != 63))
        {
            words++;
        }
        else if (text[i] == '.' || text[i] == '!' || text[i] == '?')
        {
            sentences++;
            words++;
        }
    }

    printf("\nLetters: %i\nWords: %i\nSentences: %i\n", letters, words, sentences);

// Coleman-Liau index computation using the formula

    float L = (letters / (float) words) * 100;
    printf("Average No. of letters: %f\n", L);
    float S = (sentences / (float) words) * 100;
    printf("Average No. of sentences: %f\n", S);
    float index = round(0.0588 * L - 0.296 * S - 15.8);

// Finally outputs the result to the user

    if (index < 1)
    {
        printf("Before Grade 1\n");
    }
    else if (index >= 16)
    {
        printf("Grade 16+\n");
    }
    else
    {
        printf("Grade %i\n", (int) index);
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900