How to print like this?

J

JOYCE

Hello!I'm a new learner.
I want to make the programme like this:give user a passage named
input,count the times of the words appearing in the passage,and print
the answer.but there is some errors that I can't find. The following
is my CPP.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>

char *convert(char a[]);
int differ(int a,char array[]);

int main(){
FILE *openptr;
char *tokenPtr;
char *word [100];
char input [500] = {NULL};
int count [] = {0};
char file [10];

printf("Enter input file name: ");
scanf("%s", &file);

int i = 0;
openptr = fopen(file,"r");
while((input = fgetc(openptr)) != -1){
i++;
}

fclose(openptr);
printf("%s", input);
printf("\n%s\t%s\n","word","appear times");

tokenPtr = strtok(input," ");
int t = 0;
while(tokenPtr != NULL){
word[t] = tokenPtr;
tokenPtr = strtok(NULL," ");
t++;
}

for(i=0; i<=t; i++){
word[t] = convert(word );
}
for(t=0; word[t]!=NULL; t++){
if(differ(t,word[t])==0){
count [t] = 1;
}
else{
count[differ(t,word[t])]++;
count[t]=0;
word[t]=NULL;
}
}

for(t=0; t<=100; t++){
if(word[t]!=NULL){
printf("%s\t",word[t]);
printf("%d\n",count[t]);
}
}

system("pause");

return 0;
}

char* convert(char a[]){
int i;
char b [] = {NULL};
char *answer = b;

for(i=0; a!='\0'; i++){
if(ispunct(a) != 0){
b=a;
}
}

return answer;
}

int differ(int a,char array[]){
int i;
int b=0;

for(i=0; i<=a-1; i++){
if(array == array[a]){
b++;
}
}

if(b == 0)
return 0;
else
return 1;
}
who can help me?thanks.
 
B

Barry Schwarz

Hello!I'm a new learner.
I want to make the programme like this:give user a passage named
input,count the times of the words appearing in the passage,and print
the answer.but there is some errors that I can't find. The following

Don't you think it would help if you told what the problems were? Are
we supposed to guess?
is my CPP.

cpp is usually used for C++ source. If you want your compiler to
process it as a C program, the normal convention is .c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>

char *convert(char a[]);
int differ(int a,char array[]);

int main(){
FILE *openptr;
char *tokenPtr;
char *word [100];
char input [500] = {NULL};

NULL is a pointer constant. It can be defined as (void*)0. In this
case, the above is a constraint violation because you cannot assign a
pointer value to a char. You should be using '\0'.
int count [] = {0};

This is a one element array. The only element is count[0] which has a
value of 0.
char file [10];

Even using old 8.3 file names, 9 characters is insufficient.
printf("Enter input file name: ");
scanf("%s", &file);

What exactly did you input here?
int i = 0;

Unless you have C99, declarations need to precede statements.
openptr = fopen(file,"r");

The messed up indenting appears to be the result of mixing tabs and
spaces. Tabs don't work to well in Usenet.

You should always check the return for fopen to determine success.
while((input = fgetc(openptr)) != -1){


On error or end of file, fgetc will return EOF. It need not be -1.
i++;
}

fclose(openptr);
printf("%s", input);

Unless the file contains 500 or more characters, input will contain a
string. But the last character in the string before the '\0' will be
-1. What do you expect that to print as?
printf("\n%s\t%s\n","word","appear times");

tokenPtr = strtok(input," ");
int t = 0;
while(tokenPtr != NULL){
word[t] = tokenPtr;
tokenPtr = strtok(NULL," ");
t++;

You need to make sure t never exceeds the number of elements in word.
}

for(i=0; i<=t; i++){
word[t] = convert(word );


What is the purpose of storing many different addresses in the single
element word[t]?
}
for(t=0; word[t]!=NULL; t++){

word was not initialized at its definition. No assignment ever stores
the value NULL in any element of word. The second expression will
always be true for any element which has been assigned a value. For
any element that hasn't been assigned a value, the clause invokes
undefined behavior because the value is indeterminate.
if(differ(t,word[t])==0){

differ() does not compare words.
count [t] = 1;

As soon as t exceeds 0, this invokes undefined behavior by writing
beyond the end of count.
}
else{
count[differ(t,word[t])]++;

The only two values differ can return are 0 and 1. You will only
increment one of these two elements (except that only one exists).
count[t]=0;
word[t]=NULL;
}
}

for(t=0; t<=100; t++){
if(word[t]!=NULL){
printf("%s\t",word[t]);
printf("%d\n",count[t]);
}
}

system("pause");

return 0;
}

char* convert(char a[]){
int i;
char b [] = {NULL};

This creates a 1 element array. The only element is b[0].
char *answer = b;

for(i=0; a!='\0'; i++){
if(ispunct(a) != 0){
b=a;


Here you invoke undefined behavior as soon as i is greater than 0 by
storing beyond the end of b.
}
}

return answer;

You return the address of an automatic object. The object goes out of
existence as soon the function ends. Any attempt by the calling
program to evaluate or dereference this address (which it does)
invokes undefined behavior.
}

int differ(int a,char array[]){
int i;
int b=0;

for(i=0; i<=a-1; i++){
if(array == array[a]){


This compares all previous characters to array[a]. You are supposed
to be checking for duplicate words, not duplicate letters..
b++;
}
}

if(b == 0)
return 0;
else
return 1;
}
who can help me?thanks.

You need to rework some of your algorithms and fix your coding errors.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,262
Messages
2,571,056
Members
48,769
Latest member
Clifft

Latest Threads

Top