By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 833 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

i need your help Banfa!!!!

P: 11
the problem is to count the letters and sequence from a file. the fist thing of my code is to read file and in the main function i create is to count the characters and sequence by using 2 dimension array.
where the character include letters and spaces.
I am just learning c language at the moment, i have problem with following program:
#define TRUE 1
#define FALSE 0
#define MAX_LENGTH 100000 /* Max number of chars read from file */
#include <stdio.h>
#include <ctype.h>

int Get_String_From_File(char filename[], char input[])
/* Reads in characters (including \n) from a file as one long string */
/* filename should give name of an existing file;
input will return contents of that file */
/* Return value 0 if file open fails, 1 otherwise */
/* Maximum length of string is stored in MAX_LENGTH */

{
int i = -1; /* will be updated before being used */
char temp;
FILE *infile;

infile = fopen(filename,"r");
if (infile == NULL) {
printf("Failed to open file \"%s\"\n",filename);
return FALSE;
}

do {
i++;
input[i] = fgetc(infile);
} while (input[i] != EOF && i < MAX_LENGTH - 1 && input[i] != '\0');
if (i == MAX_LENGTH - 1) /* only reason for stopping is MAX_LENGTH */
fprintf(stderr,"Warning: Reached maximum size of file handled by this program!\n\n");
input[i] = '\0'; /* end of string marker*/
fclose(infile);

return TRUE; /* Successfully read file */
}
int main(void)
{
int c, i, j, spaces,charArray[26][26];

for (i=0; i<26; ++i) /* init array to zero */
charArray[i][j] = 0;
for( j= 0; j<26; ++j)
charArray[j] = 0;
while((c = getchar()) != EOF) /* count the letter */
if(isupper(c) && islower(c))
++char[c -> 'A' && c -> 'a' && c-> ];
for (i=0; i<26; ++i)
if(i%6==0)
for ( j= 0; j<26; ++j)
if(j%6==0) {
printf("\n");
printf("%4c%4c:%3d%3d", 'A' || 'a' +i +j, letter[i][j]);
}
printf("\n\n");
return 0;
}
Nov 1 '06 #1
Share this Question
Share on Google+
14 Replies


10K+
P: 13,264
the problem is to count the letters and sequence from a file. the fist thing of my code is to read file and in the main function i create is to count the characters and sequence by using 2 dimension array.
where the character include letters and spaces.
I am just learning c language at the moment, i have problem with following program:
#define TRUE 1
#define FALSE 0
#define MAX_LENGTH 100000 /* Max number of chars read from file */
#include <stdio.h>
#include <ctype.h>

int Get_String_From_File(char filename[], char input[])
/* Reads in characters (including \n) from a file as one long string */
/* filename should give name of an existing file;
input will return contents of that file */
/* Return value 0 if file open fails, 1 otherwise */
/* Maximum length of string is stored in MAX_LENGTH */

{
int i = -1; /* will be updated before being used */
char temp;
FILE *infile;

infile = fopen(filename,"r");
if (infile == NULL) {
printf("Failed to open file \"%s\"\n",filename);
return FALSE;
}

do {
i++;
input[i] = fgetc(infile);
} while (input[i] != EOF && i < MAX_LENGTH - 1 && input[i] != '\0');
if (i == MAX_LENGTH - 1) /* only reason for stopping is MAX_LENGTH */
fprintf(stderr,"Warning: Reached maximum size of file handled by this program!\n\n");
input[i] = '\0'; /* end of string marker*/
fclose(infile);

return TRUE; /* Successfully read file */
}
int main(void)
{
int c, i, j, spaces,charArray[26][26];

for (i=0; i<26; ++i) /* init array to zero */
charArray[i][j] = 0;
for( j= 0; j<26; ++j)
charArray[j] = 0;
while((c = getchar()) != EOF) /* count the letter */
if(isupper(c) && islower(c))
++char[c -> 'A' && c -> 'a' && c-> ];
for (i=0; i<26; ++i)
if(i%6==0)
for ( j= 0; j<26; ++j)
if(j%6==0) {
printf("\n");
printf("%4c%4c:%3d%3d", 'A' || 'a' +i +j, letter[i][j]);
}
printf("\n\n");
return 0;
}
What if Banfa has gone away for fishing trip in the Victoria falls and decides to spend a month there and there are half a dozen people who can help you who are around but seeing as they would that they are not Banfa...
Nov 1 '06 #2

Banfa
Expert Mod 5K+
P: 8,916
What if Banfa has gone away for fishing trip in the Victoria falls and decides to spend a month there and there are half a dozen people who can help you who are around but seeing as they would that they are not Banfa...
Very true, I suspect r035198x probably could have answered you problem.

Anyway you have given the specification of the program you are writing, you have given the code, you do not seem to have stated what the problem you are having is.
Nov 1 '06 #3

10K+
P: 13,264
Very true, I suspect r035198x probably could have answered you problem.

Anyway you have given the specification of the program you are writing, you have given the code, you do not seem to have stated what the problem you are having is.
Hopefully you won't be taking a holiday away from the internet of course Banfa!
I'm just wondering what the OP is trying to do with a statement like
Expand|Select|Wrap|Line Numbers
  1. if(isupper(c) && islower(c)) 
Nov 1 '06 #4

P: 11
I mean the capital and small letter doesnt count as seperately!
Nov 2 '06 #5

Banfa
Expert Mod 5K+
P: 8,916
if(isupper(c) && islower(c))

Read it as English, it says

"If c is an uppercase letter and c is a lowercase letter then do something"

Pretend c is the letter g, will this or wont this do something


Also I am intrigued to know if this line of code actually compiles and what you are trying to achieve with it

++char[c -> 'A' && c -> 'a' && c-> ];
Nov 2 '06 #6

10K+
P: 13,264
if(isupper(c) && islower(c))

Read it as English, it says

"If c is an uppercase letter and c is a lowercase letter then do something"

Pretend c is the letter g, will this or wont this do something


Also I am intrigued to know if this line of code actually compiles and what you are trying to achieve with it

++char[c -> 'A' && c -> 'a' && c-> ];
Yes. Intrigued is certainly the word for it.
Nov 2 '06 #7

P: 11
The code:
++char[c -> 'A' && c -> 'a' && c-> ];
I want the code do the following things:
character can be counted as they are capital letter and small letter and spaces.
But the gcc compiler said this is parse errors before 'A', and 'a', and' ' . I don't know how to correct it!
Nov 3 '06 #8

Banfa
Expert Mod 5K+
P: 8,916
I assume you meant to use charArray instead of char then in

++char[c -> 'A' && c -> 'a' && c-> ];

However

Expand|Select|Wrap|Line Numbers
  1. for (i=0; i<26; ++i) /* init array to zero */
  2.     charArray[i][j] = 0;
  3. for( j= 0; j<26; ++j) 
  4.     charArray[j] = 0;
  5.  
does not initialise charArray to 0. The first attempt to set charArray[i][j] happens before j has been set to anything, in the second attempt charArray[j] is no an l-value so I doubt this compiles.

Think about what you need to do to access each member of charArray in turn.

Also it is not clear why you have chosen to use a 2D array


back to

++char[c -> 'A' && c -> 'a' && c-> ];

So I am going to assume that you meant charArray and that charArray is in fact singly dimensioned.

You have a character of interest c and you want to increment one value in the array charArray to reflect this.

you are going to need a line something like

charArray[ix]++;

to increment the array value, which leaves calculating ix given c, valid ranges of c (in ASCII) are

'a' <= c <= 'z'
'A' <= c <= 'Z'
c == ' '

That is 53 different characters in total so ix will have to have the range 0-52.

So your task is to produce a method to calculate ix in the range 0 - 52 given c in the valid ranges I just gave.
Nov 3 '06 #9

P: 11
But if I only use single array I can't count the sequence? And the question need 2 D array!
Nov 6 '06 #10

P: 11
Please some one help me! the first thing of the problem is to count letters, regardless about the capital or small and spaces, cormas, numbers as one catagories that means the array side are 27 second things is to count sequences, the 2 letters that are together for example aa, ab, ac etc that means I need a 2 D array, But I don't know how to correct my code. So some one please help me!!!!
Nov 8 '06 #11

Banfa
Expert Mod 5K+
P: 8,916
Actually then you need a 1D array to count ocurances and a 2D array to count sequences.

Start with just counting the letters, we can get onto sequences. My last post had some rather strong hints on how to do this.

If you are still having trouble then post your current code (assuming it has changed) with the compile errors and indicate why you are unable to correct the errors (if there are a lot of errors only list the first few).
Nov 8 '06 #12

P: 11
The first fist half of the code are reading the File, that is correct, so I just post my main function which is incorrect somehow:
int main(void)
{
int count, c, i, spaces, char[27];
for (i = 0; i < 27; ++i) {
count = 0;
while((c=getchar()) != EOF) /* count the letter */
if(isupper(c) && islower(c))

charArray[i]++;
while('a'<=c<='z'
'A' <= c <= 'Z'c == ' ')
for(i=0;i<26;++i)
if(i%6==0)
{
printf("\n");
printf("%4c:%3d", 'A' || 'a' +i , char[i]);
}
printf("\n\n");
return 0;
}
And I don't know how can I correct my 2d array:
int main ()
int count,c, i, j, charArray[26][26];
for (i = 0; i < 26; ++i) {
count = 0;
for( j= 0; j<26; ++j)
count = 0;
while((c=getchar()) != EOF) /* count the letter */
if(isupper(c) && islower(c))
for(i=0;i<26;++i)
if(i%6==0)
for(j=0;j<26;++j)
if(j%6==0){
printf("\n");
printf("%4c%4c:%3d%3d", 'A' || 'a' +i +j, char[i][j]);
}
printf("\n\n");
return 0;
}
Nov 10 '06 #13

P: 11
Can you Help me!!!
The first fist half of the code are reading the File, that is correct, so I just post my main function which is incorrect somehow:
int main(void)
{
int count, c, i, spaces, char[27];
for (i = 0; i < 27; ++i) {
count = 0;
while((c=getchar()) != EOF) /* count the letter */
if(isupper(c) && islower(c))

charArray[i]++;
while('a'<=c<='z'
'A' <= c <= 'Z'c == ' ')
for(i=0;i<26;++i)
if(i%6==0)
{
printf("\n");
printf("%4c:%3d", 'A' || 'a' +i , char[i]);
}
printf("\n\n");
return 0;
}
And I don't know how can I correct my 2d array:
int main ()
int count,c, i, j, charArray[26][26];
for (i = 0; i < 26; ++i) {
count = 0;
for( j= 0; j<26; ++j)
count = 0;
while((c=getchar()) != EOF) /* count the letter */
if(isupper(c) && islower(c))
for(i=0;i<26;++i)
if(i%6==0)
for(j=0;j<26;++j)
if(j%6==0){
printf("\n");
printf("%4c%4c:%3d%3d", 'A' || 'a' +i +j, char[i][j]);
}
printf("\n\n");
return 0;
}
Thank you for any suggestion
Nov 13 '06 #14

Banfa
Expert Mod 5K+
P: 8,916
I suggest that you fix all the compil errors this code produces.

As I have already said if you are getting compiler errors that you do not understand or are unable to fix then post the error indicating on which line of code they are happening, however here are a few tips

None of these are correct C syntax

int count, c, i, spaces, char[27];

while('a'<=c<='z'
'A' <= c <= 'Z'c == ' ')

printf("%4c:%3d", 'A' || 'a' +i , char[i]);


Trying to redefine main in the middle of main is just wrong.
Nov 13 '06 #15

Post your reply

Sign in to post your reply or Sign up for a free account.