473,396 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

i need your help Banfa!!!!

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
14 1723
r035198x
13,262 8TB
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
9,065 Expert Mod 8TB
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
r035198x
13,262 8TB
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
I mean the capital and small letter doesnt count as seperately!
Nov 2 '06 #5
Banfa
9,065 Expert Mod 8TB
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
r035198x
13,262 8TB
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
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
9,065 Expert Mod 8TB
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
But if I only use single array I can't count the sequence? And the question need 2 D array!
Nov 6 '06 #10
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
9,065 Expert Mod 8TB
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
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
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
9,065 Expert Mod 8TB
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

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

Similar topics

0
by: Gregory Nans | last post by:
hello, i need some help to 'tree-ify' a string... for example i have strings such as : s = """A(here 's , B(A ) silly test) C(to show D(what kind) of stuff i need))""" and i need to...
0
by: xunling | last post by:
i have a question about answering ..... this topic is "need help" what do i have to write at te topic line, !after i have klicked the "answer message" button ive tried many possibilities,...
2
by: Paul Mendez | last post by:
I really need some help Date Code ConCAT Bal_Fwd NS_Fees Amt_Coll Cur_End_Bal 1/15/2004 KW 11KW2003 $500.00 $250.00 $250.00 2/15/2004 KW 12KW2003 $300.00 $500.00 ...
7
by: Timothy Shih | last post by:
Hi, I am trying to figure out how to use unmanaged code using P/Invoke. I wrote a simple function which takes in 2 buffers (one a byte buffer, one a char buffer) and copies the contents of the byte...
8
by: Elliot M. Rodriguez | last post by:
I am having a heckuva time debugging this, or determining why my page is behaving this way. I have a search form, that when completed, returns a datagrid. When the user selects a row (normal...
3
by: Rich Squid | last post by:
Hello Here's my basic problem: On my asp.net form page I have a DetailsView (default mode=edit) bound to a AccessDataSource control. Users can successfuly update a databound template field,...
2
by: XML Beginner | last post by:
I have an XML file that contains values that my application needs, so it knows which database to connect to. It also contains a configuration option so that I can specify which node to return...
2
by: =?Utf-8?B?am9lb2ppaA==?= | last post by:
Most students do not have enough time to do their homeworks, assignments, college papers, etc alone. Also most of them, even though they know what to do with respect to their writing assignment,...
30
by: carlos123 | last post by:
Ok I am working on a Hall Pass program for my computer programming class. There are 3 things that I am confused on. 1. Reading a file. 2. Taking that data read from the file and putting it into...
1
by: jhaydon | last post by:
First of all, I'm not a CSS expert. If I was, I wouldn't need to be posting for help here. Secondly, I have been doing web design for several years, just not css. Thirdly, I need help and hope...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.