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.
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.