/*	program		: array.c
	last updated	: 2018/07/14		*/

#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "stdbool.h"
#include "ctype.h"

#define	or		||
#define	and		&&

#define	BOLD	printf("\x1b[1m")
#define	RESET	printf("\x1b[0m")

#define	NEW_LINE	printf("\n")

#define	MNOC	128		// 英単語の最大文字数 maximum number of character

const char EOF_STR[] = "***END***";
char	g_STR[MNOC];
int		g_CNT = 0;
bool	g_FLAG = false;

struct _Array{
	char word[MNOC];		// 英単語
	int count;	// 頻出頻度
} Array[10000];

/**************************************************************************************
 *	SpecialRemove
 *				--	英単語の最後の文字がアルファベット,数字以外であればそれを除く
 *	パラメータ
 *		str		-- 英単語
 *		len		-- 英単語の文字列長
 *
 *	補足
 *		isalpha	-- 文字がアルファベットか判定		ヘッダファイル:ctype.h
 *		isdigit	-- 文字が数字か判定				ヘッダファイル:ctype.h
 **************************************************************************************/

void SpecialRemove(char *str){
	int len = strlen(str);
	if (strcmp(str,EOF_STR) == 0) return;
	if (isalpha(str[len-1]) or isdigit(str[len-1])) return;
	else str[len-1] = '\0';
}

// データ入力関数
void Input(){
	scanf("%s",g_STR);
	SpecialRemove(g_STR);
}

// データ出力関数
void Output(){
	int i = 0;
	BOLD;
	while ( i < g_CNT ) {
		printf("%3d\t%s\n", Array[i].count, Array[i].word);
		i++;
	}
	RESET;
}

// 入力した単語がすでに既存しているか判定
int Compare(){
	int i = 0;
	while ( i < g_CNT ) {
		if (strcmp(g_STR, Array[i].word) ==0) {
			g_FLAG = true;
			return i;
		}
	}
	return false;
}

// メインループ
void MainLoop(){
	int num;
	while (strcmp(g_STR,EOF_STR) != 0) {
		num = Compare();
		if (g_FLAG == true) {
			Array[num].count++;
			g_FLAG = false;
		} else {
			strcpy(Array[g_CNT].word, g_STR);
			Array[g_CNT].count = 1;
			g_CNT++;
		}
		Input();
	}
}

// メイン関数
int main(void){
	Input();
	if (strcmp(g_STR,EOF_STR) == 0) exit(0);
	MainLoop();
	
	NEW_LINE;
	Output();
	
	exit(EXIT_SUCCESS);
}


入力データ ( data.txt として保存 ) 終了判定文字列は「 ***END*** 」


Until about a decade ago, American newspapers had a clear idea what the "news" was. It was what the prime ministers and presidents of leading countries announced at press conferences. It was politics and diplomacy and cabinet shuffles. It was economic statistics and business mergers and Wall Street. But over the last dozen years, there has been a growing movement in American newspapers away from that conception of news and toward something looser, more feature-oriented, and more trend-driven. Max Frankel, who ruled as executive editor of the New York Times from 1986 through 1994 and thus helped set the journalistic agenda, pioneered the concept of soft features on the front page. Frankel wanted newspapers to be read, and he argued that articles about country music and miniskirts were not only engaging but also reflected important trends: Many readers probably cared more about hemlines than about arms-control talks. Under Frankel, the Times also began to search for new and compelling ways to cover foreign affairs in the post-Cold War world. Editors felt that politics mattered less than before, and that correspondents should write increasingly about the people of foreign countries rather than just the governments. Correspondents were pushed to spend less time in the national capitals talking to cabinet ministers and more time in rural areas talking to farmers and homemakers. There was more attention to trends; indeed, Frankel emphasized that "sometimes the most important news doesn't happen on any one day." Other newspapers, particularly the Washington Post and Los Angeles Times, pursued a similar approach. They all beefed up their science and medical reporting, and soon nobody blinked when stories about health or fitness―Does vitamin C prevent colds? Does weightlifting help shed pounds as well as running does?―were promoted to the front page. The Wall Street Journal led the way in pioneering skeptical reporting about corporations, and soon all major newspapers were assigning investigative reporters to look at corporate executives as well as politicians. Yet critics complained that while newspapers were becoming perhaps more readable, they were also becoming somewhat dispensable. An article about vitamin C might be engaging, but it did not matter if you missed it. Front pages were becoming clogged with so many articles about fascinating people that it was difficult to tell what, if anything, had happened that was truly of any consequence. Moreover, as the Internet joined television in providing the public with an alternative source of information, many young people simply spurned newspapers altogether. The debate remains unresolved. Newspaper executives do worry that their relevancy may decrease as they reduce their coverage of hard news, but they say that in a world of 24-hour news on the Internet and cable television, they simply cannot compete effectively at being the first to inform the public of the latest events. They must take a different approach. ***END***




実行結果


user$ gcc array.c
user$ ./a.out < data.txt

1 Until
9 about
6 a
1 decade
1 ago
2 American
7 newspapers
2 had
1 clear
1 idea
3 what
22 the
1 "news
7 was
3 It
1 prime
2 ministers
19 and
1 presidents
10 of
1 leading
2 countries
1 announced
3 at
1 press
1 conferences
2 politics
1 diplomacy
2 cabinet
1 shuffles
1 economic
1 statistics
1 business
1 mergers
2 Wall
2 Street
1 But
1 over
1 last
1 dozen
1 years
1 there
1 has
1 been
1 growing
1 movement
7 in
1 away
2 from
10 that
1 conception
4 news
1 toward
1 something
1 looser
6 more
1 feature-oriented
1 trend-driven
1 Max
4 Frankel
1 who
1 ruled
7 as
1 executive
1 editor
1 New
1 York
3 Times
1 1986
1 through
1 1994
1 thus
1 helped
1 set
1 journalistic
1 agenda
1 pioneered
1 concept
1 soft
1 features
3 on
2 front
2 page
1 wanted
11 to
2 be
1 read
1 he
1 argued
2 articles
1 country
1 music
1 miniskirts
6 were
2 not
1 only
2 engaging
3 but
3 also
1 reflected
2 important
2 trends
1 Many
1 readers
1 probably
1 cared
1 hemlines
3 than
1 arms-control
1 talks
1 Under
1 began
1 search
1 for
1 new
1 compelling
1 ways
1 cover
2 foreign
1 affairs
1 post-Cold
1 War
2 world
1 Editors
1 felt
1 mattered
2 less
1 before
1 correspondents
1 should
1 write
1 increasingly
3 people
1 rather
1 just
1 governments
1 Correspondents
1 pushed
1 spend
2 time
1 national
1 capitals
2 talking
1 rural
1 areas
1 farmers
1 homemakers
1 There
1 attention
1 indeed
1 emphasized
1 "sometimes
1 most
1 doesn't
1 happen
2 any
1 one
1 day.
1 Other
1 particularly
1 Washington
1 Post
1 Los
1 Angeles
1 pursued
1 similar
2 approach
2 They
2 all
1 beefed
1 up
3 their
1 science
1 medical
2 reporting
2 soon
1 nobody
1 blinked
1 when
1 stories
1 health
1 or
1 fitness―Does
2 vitamin
2 C
1 prevent
1 colds
1 Does
1 weightlifting
1 help
1 shed
1 pounds
2 well
1 running
1 does?―were
1 promoted
2 The
1 Journal
1 led
1 way
1 pioneering
1 skeptical
1 corporations
1 major
1 assigning
1 investigative
1 reporters
1 look
1 corporate
2 executives
1 politicians
1 Yet
1 critics
1 complained
1 while
3 becoming
1 perhaps
1 readable
4 they
1 somewhat
1 dispensable
1 An
1 article
1 might
3 it
1 did
1 matter
2 if
1 you
1 missed
1 Front
1 pages
1 clogged
2 with
1 so
2 many
1 fascinating
1 difficult
1 tell
1 anything
1 happened
1 truly
1 consequence
1 Moreover
2 Internet
1 joined
2 television
1 providing
2 public
1 an
1 alternative
1 source
1 information
1 young
2 simply
1 spurned
1 altogether
1 debate
1 remains
1 unresolved
1 Newspaper
1 do
1 worry
1 relevancy
1 may
1 decrease
1 reduce
1 coverage
1 hard
1 say
1 24-hour
1 cable
1 cannot
1 compete
1 effectively
1 being
1 first
1 inform
1 latest
1 events
1 must
1 take
1 different