[Solved] Last word is Failing …?


Try this:

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

FILE *fr; 

int main (int argc, const char * argv[]){
  char line[1024];
  double uppercase = 0, lowercase = 0;
  int x = 0;
  fr = fopen ("PercenUpLow.txt", "rt");
  if (fr == NULL) {
    printf ("Can't open file") ;
    return EXIT_FAILURE ;
  }
  while(fgets(line, 1024, fr) != NULL) {
    do {
      if ((line[x] >= 'A') && (line[x] <= 'Z')){
        uppercase += 1;
      }else if ((line[x] >= 'a') && (line[x] <= 'z')){
        lowercase += 1;    
      }
      x+=1;
    } while (line[x] != '\n' && line[x] != 0);

    printf("lowercase: %0.2f ", (lowercase / x) * 100);
    printf("uppercase: %0.2f \n", (uppercase / x) * 100);
    lowercase = 0;
    uppercase = 0;
    x = 0;
  }    
  return EXIT_SUCCESS;
}

I changed this line:

     } while (line[x] != '\n' && line[x] != 0);

If the file is not terminated by a \n, then the last line read is not terminated by a \n either (seems obvious) and you are reading further than the end of the line, leading to undefined behaviour.

I also test if the file could be opened correctly.

You should definitely learn how to use a debugger.

solved Last word is Failing …?