Counting characters from multiple files .

Program reads a file name from keyboard and counts number of distinct

Page 1 of 1

1 Replies - 977 Views - Last Post: 06 June 2009 - 06:39 AM Rate Topic: -----

#1 bretuoba   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 06-May 09

Counting characters from multiple files .

Post icon  Posted 06 June 2009 - 06:33 AM

I am trying to write code for Program reads a file name from keyboard and counts number of distinct characters in the file and ask user to input another file. My code worked when i was reading from a single file but when i tried modify it to read multiple files it fails. Any help will be appreciated. Is my first time seeking here here. Thanks seniors.

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


int main()
{
	char name[20];
	char ans='Y';
	FILE *fp;
	char *ptr;
	int U[26];
	int L[26];
	int D[10];
	int i,j,c,extra;

	  printf("\n Please input name of files,file name should not exceed 20 characters\n");
				scanf("%s",name);

		for( i=0;i<26;i++); /*intializes counters*/
			 {

			U[i]=0;
			L[i]=0;

			}
			 for( j=0;j<26;j++);
			 {

			D[i];

			}



		do{
		if((fp=fopen(name,"r"))==NULL)

			printf("\nError! in reading file\n");

		else

	{
				fputs(ptr,fp);
				 fclose(fp);
	while(!feof(fp))
		{
				c=fgetc(fp);
					  if(c>='A' && c<='Z'){
					++U[c-'A'];
				}
				else
					if(c>='a' && c<='z'){
					++L[c-'a'];
				}
				else
							 if(c>='0' && c<='9'){
					++D[c-'A'];
				}
				else{
					extra++;
								}
		for( i=0;i<26;i++);
			 {
			printf("\nThe upper case letters %c appear %d respectively\n",'A'+i,U[i]);
		}
	   for( i=0;i<26;i++);
			 {
			printf("\nThe lower case letters %c appear %d respectively\n",'a'+i,L[i]);
		}
	  for( i=0;i<10;i++);
			 {
			printf("\nThe digits %c appear %d respectively\n",'0'+i,D[i]);
		}
		break;

			}
		}

	printf("\nDo you want to enter another file name?[Y or N]\n");
	scanf("%c",&ans);

		}while(toupper(ans)!='N');



return 0;
}




Is This A Good Question/Topic? 0
  • +

Replies To: Counting characters from multiple files .

#2 janotte   User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Counting characters from multiple files .

Posted 06 June 2009 - 06:39 AM

Welcome to DIC!

First question is what is this line supposed to do?
D[i];

When you say it fails after adding multiple file support do you mean it stopped working correctly when you added the do-while() loop?

Or do you mean something else?

In what way does it fail to do what you want/expect?

EDIT:
I have tried to have a look at your code but it is fairly confusing as you have presented it.

Please choose one of the two main indentation and brace standards (either opening brace on same line as command statement OR opening brace on the next line after the command statement) and apply it consistently.

It doesn't matter which one you choose but mixing the two styles is very bad form and makes your code very hard to read.

I'd very much like to see you enclose the scope of all your else statements inside opening and closing braces "{ }" to make it clear to yourself and others where you think that scope is.

This post has been edited by janotte: 06 June 2009 - 06:58 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1