输入20 个单词按照字母排序,并统计单词个数 c语言编程,C语言,输入一句话,分离出单词,并统计每个单词出现的次数()...

C语言,输入一句话,分离出单词,并统计每个单词出现的次数()0

dcebd7a0de6265b6ccae5ead692f1eab.pngadee4996492017.01.18浏览118次分享举报

#include

#include

#define Num 100

char Sent();

char Word();

int NumNJudge();

void main()

{

/*输入一段英文,存进对应的数组*/

printf("请输入一段英文(包含空格逗号句号和英文字母):");

Sent;

printf("\n");

/*分离出单词并且存放进字符串数组、输出字符串数组(Word函数中嵌套了Number&Judge函数)*/

Word;

}

/***************************** ***************/ /********************************************/

/********************************************/ /********************************************/

/********************************************/ /********************************************/

/*Sentence函数定义*/

char Sent(char a[Num],char str[Num],int i,int j)

{

for(i=0; i < Num ; i++)

{

gets (str);

strcpy(a[i],str);

}

/*****/

}

/*Word函数定义(嵌套调用Number&Judge函数)*/

char Word(int i,char a[Num],char str[Num])

{

if(str[i]!='\0' && str[i]!=',' && str[i]!='.')

{

strcpy(a[i],str);

i++;

puts (str);

NumNJudge;

printf(" ");

}

else

i++;

}

/*****/

/*Number&Judge函数定义*/

int NumNJudge(char a[Num],int num,char str[Num],int i,int j)

{

num = 0;

for(i = 0 ; i < Num ; i++)

for(j = 0 ; j < Num ; j++)

{

if(a[i] == a[j])

{

j = i;

num++;

}

puts(a[i]);

printf("(%d)",num);

}

}

/*****/

(结果错误显示warning C4551:function call missing argument list;

C2664: 'strcpy' : cannot convert parameter 1 from 'char' to 'char *';

C2664: 'puts' : cannot convert parameter 1 from 'char' to 'const char *')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以通过以下步骤来统计文件中每个单词出现次数: 1. 读取文件:使用Java的文件读取功能,将待统计的文件读入内存中。 2. 分离单词:将读入的文件内容按照空格、标点符号等分离单词,可以使用Java的正则表达式来实现。 3. 统计单词数量:使用HashMap来存储每个单词出现次数,遍历每个单词,如果该单词已经在HashMap中存在,则将其出现次数加1,否则将该单词加入HashMap,并将其出现次数设为1。 4. 输出结果:遍历HashMap,输出每个单词及其出现次数。 以下是示例代码: ```java import java.io.BufferedReader; import java.io.FileReader; import java.util.HashMap; import java.util.Map; public class WordCount { public static void main(String[] args) { String filename = "test.txt"; // 待统计的文件名 Map<String, Integer> wordCount = new HashMap<>(); // 用于存储单词出现次数的HashMap // 读取文件 try (BufferedReader reader = new BufferedReader(new FileReader(filename))) { String line; while ((line = reader.readLine()) != null) { // 分离单词 String[] words = line.split("[\\p{Punct}\\s]+"); for (String word : words) { // 统计单词数量 if (wordCount.containsKey(word)) { wordCount.put(word, wordCount.get(word) + 1); } else { wordCount.put(word, 1); } } } } catch (Exception e) { e.printStackTrace(); } // 输出结果 for (Map.Entry<String, Integer> entry : wordCount.entrySet()) { System.out.println(entry.getKey() + ": " + entry.getValue()); } } } ``` 这个示例代码可以读取名为test.txt的文件,统计其中每个单词出现次数,并输出结果。你可以将其修改成符合你自己需求的代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值