C语言下,输入一个单词,找出单词中出现次数最多的字母的出现次数
时间: 2024-01-22 20:14:30 浏览: 49
以下是C语言的代码,可以输入一个单词,找出单词中出现次数最多的字母的出现次数:
```c
#include <stdio.h>
#include <string.h>
int main()
{
char word[100];
int count[26] = {0}, max_count = 0;
printf("请输入一个单词:");
scanf("%s", word);
int length = strlen(word);
for (int i = 0; i < length; i++) {
count[word[i] - 'a']++;
}
for (int i = 0; i < 26; i++) {
if (count[i] > max_count) {
max_count = count[i];
}
}
printf("出现次数最多的字母出现了 %d 次。\n", max_count);
return 0;
}
```
相关问题
,给了一个单词后,帮助他找到出现最多的字母和这 个字母出现的次数用C语言写一段代码
### 回答1:
嗯,我可以给你一个参考:#include <stdio.h>int main()
{
char str[100];
int i, j, count;
char maxChar;
int maxCount = 0;
printf("请输入一个单词:");
scanf("%s", str);
for (i = 0; str[i] != '\0'; ++i)
{
count = 1;
for (j = i + 1; str[j] != '\0'; ++j)
{
if (str[i] == str[j])
++count;
}
if (count > maxCount)
{
maxCount = count;
maxChar = str[i];
}
}
printf("字母 %c 出现最多,次数为 %d。", maxChar, maxCount);
return 0;
}
### 回答2:
#include <stdio.h>
#include <string.h>
void findMostFrequentChar(char word[]) {
int count[26] = {0};
int maxCount = 0;
char maxChar;
for (int i = 0; i < strlen(word); i++) {
if (word[i] >= 'a' && word[i] <= 'z') {
count[word[i] - 'a']++;
}
else if (word[i] >= 'A' && word[i] <= 'Z') {
count[word[i] - 'A']++;
}
}
for (int i = 0; i < 26; i++) {
if (count[i] > maxCount) {
maxCount = count[i];
maxChar = 'a' + i;
}
}
printf("出现最多的字母是%c,出现的次数是%d\n", maxChar, maxCount);
}
int main() {
char word[100];
printf("请输入单词:");
scanf("%s", word);
findMostFrequentChar(word);
return 0;
}
### 回答3:
下面是一个使用C语言编写的代码,该代码能够接收一个单词作为输入,并找出在单词中出现最多的字母以及该字母出现的次数。
```c
#include <stdio.h>
#include <string.h>
#define MAX_SIZE 100
int main() {
char word[MAX_SIZE];
char maxChar;
int maxCount = 0;
int i, j, count;
printf("请输入一个单词:");
scanf("%s", word);
// 遍历单词中的每个字母
for (i = 0; word[i] != '\0'; i++) {
count = 0;
// 检查当前字母在单词中出现的次数
for (j = 0; word[j] != '\0'; j++) {
if (word[i] == word[j])
count++;
}
// 更新最大次数和字母
if (count > maxCount) {
maxCount = count;
maxChar = word[i];
}
}
printf("出现最多的字母是 %c,出现次数为 %d\n", maxChar, maxCount);
return 0;
}
```
运行该代码,输入一个单词后,代码将会输出该单词中出现最多的字母以及该字母出现的次数。
用C语言写一个代码,要求输入一个单词,在文件中找出所有出现该单词的句子及单词出现的位置,并且统计该单词出现的次数
以下是一个可以实现你要求的C语言代码:
```c
#include <stdio.h>
#include <string.h>
#define MAX_SENTENCE_LENGTH 1000
#define MAX_WORD_LENGTH 50
int main()
{
char filename[100];
char word[MAX_WORD_LENGTH];
char sentence[MAX_SENTENCE_LENGTH];
char *pos;
int lineno = 0, count = 0;
printf("Enter filename: ");
scanf("%s", filename);
printf("Enter word to search: ");
scanf("%s", word);
FILE *fp = fopen(filename, "r");
if (fp == NULL)
{
printf("Failed to open file %s\n", filename);
return 1;
}
while (fgets(sentence, MAX_SENTENCE_LENGTH, fp) != NULL)
{
lineno++;
pos = strstr(sentence, word);
while (pos != NULL)
{
count++;
printf("Line %d: %s", lineno, sentence);
printf("Word '%s' found at position %d\n", word, pos - sentence + 1);
pos = strstr(pos + 1, word);
}
}
printf("The word '%s' appears %d times in the file.\n", word, count);
fclose(fp);
return 0;
}
```
这个程序会要求用户输入一个文件名和一个单词,然后在文件中查找出现该单词的句子及单词出现的位置,并且统计该单词出现的次数。程序会逐行读取文件内容,然后使用 `strstr()` 函数查找单词出现的位置,如果找到了就打印出该句子及单词出现的位置,并继续在该句子中查找单词出现的位置直到找不到为止。最后程序会打印出单词在文件中出现的次数。