-4

I tried a lot of thing but when I chose 2 to count upper case and lower case separately the result was garbage, I don't know what is wrong with this, while doesn't care if lower case or upper case it still works correctly.

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

int main()
{
  int i,h,k ,count[26] = {0}, nam[26] = {0};
  char c[1000];
  FILE *fptr;
  fptr=fopen("tep1.txt","w");
  if(fptr==NULL){
    printf("Error!");
    exit(1);
  }
  printf("a String please:\n");
  gets(c);
  fprintf(fptr,"%s",c);
  fclose(fptr);
  printf("discern between upper case and lower case? 0=no, 1=yes");
  scanf("%d",&h);

  if(h==0){    
    while (c[i] != '\0'){     
      if (c[i] >= 'a' && c[i] <= 'z') 
        count[c[i]-'a']++;

      if (c[i]>='A' &&c[i]<='Z')
        count[c[i]-'A']++;
      i++;
    }

    for (i = 0; i < 26; i++){
      if (count[i] != 0)
        printf("%c %c appears %d times on file.\n",i+'a',i+'A',count[i]);
    }
    return 0;
  }
  if(h==1){    
    while (c[i]|c[k] != '\0') {      
      if (c[i] >= 'a' && c[i] <= 'z') 
        count[c[i]-'a']++;
      i++;
      if (c[k]>='A' &&c[k]<='Z')
        nam[c[k]-'A']++;
      k++;
    }

    for (i = 0; i < 26; i++){
      if (count[i] != 0)
        printf("%c %c appears %d times on file.\n",i+'a',count[i]);      
    }

    for (k = 0; k < 26; k++){
      if (nam[k] != 0)
        printf("%c %c appears %d times on file.\n",k+'A',nam[k]);      
    }

    return 0;
  }
}
SSC
  • 1,311
  • 5
  • 18
  • 29
user3576946
  • 1
  • 1
  • 4

2 Answers2

2
  • First, you have to initialize your integers like i = 0.
  • Second thing is for count upper and lower case, problem is in printing.In print function you have written %c two times and one %d, but gives only two arguments.
  • Just replace your printf function from,

    printf("%c %c appears %d times on file.\n",i+'a',count[i]);

    to

    printf("%c appears %d times on file.\n",i+'a',count[i]); and your problem will be solved.

Sagar Patel
  • 864
  • 1
  • 11
  • 22
1

There are quite a few problems in this part of the code:

while (c[i]|c[k] != '\0') 
{
  if (c[i] >= 'a' && c[i] <= 'z') 
     count[c[i]-'a']++;
  i++;
  if (c[k]>='A' &&c[k]<='Z')
     nam[c[k]-'A']++;
  k++;
}

You don't need two counters; you simply need the two arrays count and nam. Your loop structure for parsing each letter should be the same.

Take a step back from the code, and think of what your loop structure should do. Regardless of whether you pay attention to case, you should iterate through the characters of the line in the same way.

Incidentally, the logic in the while statement is wrong. If you wanted anything at all with c[i] and c[k], you would have wanted

while (c[i] != '\0' && c[k] != '\0')

What the code you have does is bitwise-OR the values of c[i] and c[k], and see if they're equal to '\0', i.e. equal to 0. Until i and k are both pointing to a null character, this won't happen for a long time. But again, your while loop to iterate through the line should be the same as in the h == 0 case.

As far as I can tell, you don't need your k counter anywhere.

Scott Mermelstein
  • 15,174
  • 4
  • 48
  • 76