単語統計プログラム

1938 ワード

簡単な単語の統計プログラム、問題のソース:http://topic.csdn.net/u/20111114/10/2e439bbf-04c5-4042-9905-ece0bf008b97.html
 
/* 
	  :        
*/

#include <stdio.h>
#include <string.h>

main()
{
	char *t[20];			//         ,   20
	char (*t2)[20];			//       ,      20      
	printf("%d: %x, %x
", sizeof(t), t, t+1); printf("%d: %x, %x

", sizeof(t2), t2, t2+1); //return 0; int i=0,j=0,nLen; char *p,*q,str[]="My ti* me is! limi#ted.",words[100][20]; memset(words,'\0',sizeof(words)); p=str; while (*p) { //if (*p==' ' || *p=='!' || *p==',' || *p=='.' || *p=='?') if(!(isalpha(*p) || isdigit(*p))) // , { if(j > 0) // { words[i++][j]='\0'; j=0; } } else { if (j==0 && (*p >= 'a' && *p <= 'z')) { words[i][j++]=*p - 32; // } else { words[i][j++]=*p; } } p++; } // : p p=words[0]; while (strlen(p) > 0) // p { printf("p: %x\t%s
", p, p); p += 20; } printf("
"); // : p2 char (*p2)[20] = words; while (strlen((const char*)p2) > 0) // p2 { printf("p2: %x\t%s
", p2, p2); p2++; } printf("
"); // : words i = -1; while (strlen(words[++i]) > 0) // words { printf("wrods[%d]: %s
", i, words[i]); } }
コンパイル実行:
[zcm@t #109]$make
gcc -g   -c -o a.o a.c
gcc -g -o a a.o
[zcm@t #110]$./a
80: bfb5d554, bfb5d558
4: 8049a08, 8049a1c

p: bfb5cd6c	My
p: bfb5cd80	Ti
p: bfb5cd94	Me
p: bfb5cda8	Is
p: bfb5cdbc	Limi
p: bfb5cdd0	Ted

p2: bfb5cd6c	My
p2: bfb5cd80	Ti
p2: bfb5cd94	Me
p2: bfb5cda8	Is
p2: bfb5cdbc	Limi
p2: bfb5cdd0	Ted

wrods[0]: My
wrods[1]: Ti
wrods[2]: Me
wrods[3]: Is
wrods[4]: Limi
wrods[5]: Ted
[zcm@t #111]$