回复 2# 广陵绝唱 的帖子
谢谢楼上的..我自己也寫了一个程序,不知道还需要那些改进.
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define MAXWORDS 1000
#define MAXCHARS 20
int getword(char word[MAXCHARS + 1], int chars);
int main(int argc, char **argv) {
char words[MAXCHARS+1];
char text[MAXWORDS][MAXCHARS+1];
int numofwords = 0;
int i = 0, j = 0;
while(getword(words, MAXCHARS)!= EOF) {
if (numofwords < MAXWORDS) {
strcpy(text[numofwords], words);
}
numofwords++;
}
char temp[MAXWORDS + 1];
for (i = 0; i < (numofwords - 1); i++) {
for (j = (i + 1); j < numofwords; j++) {
if (strcasecmp(text[i], text[j]) > 0) {
strcpy(temp, text[i]);
strcpy(text[i], text[j]);
strcpy(text[j], temp);
}
}
}
int sum = 0;
int number = 1;
for (i = 0; i < (numofwords - 1); i++) {
if(strcasecmp(text[i],text[i + 1]) == 0) {
number++;
} else {
printf("%-10s
%5d\n", text[i],number);
sum = sum + number;
number = 1;
}
}
printf("%-10s
%5d\n", text[numofwords - 1], (numofwords - sum));
printf("Processing completed\n");
return 0;
}
int getword(char *word, int chars) {
char c;
c = getchar();
while(c != EOF && !isalpha(c)) {
c = getchar();
}
if (c == EOF) {
return EOF;
}
*word = c;
word =word + 1;
chars--;
c = getchar();
while(c != EOF && isalpha(c) && chars > 0) {
*word = c;
word = word + 1;
chars--;
c = getchar();
}
*word = '\0';
return 0;
}